mktime
time_t mktime ( struct tm * ptm ); | time.h |
cplusplus.com |
Convert tm structure to time_t value.
Checks the members of the tm structure passed as parameter
ptm adjusting the values if the ones provided are not in the possible range
or they are incomplete or mistaken and then translates that structure to a time_t
value (seconds elapsed since Jan 1, 1970) that is returned.
The original values of tm_wday and tm_yday members of ptm are
ignored and filled with the correspondent ones to the calculated date. The range of
tm_mday is not checked until tm_mon and
tm_year are determined.
Parameters.
Return Value.
A time_t value corresponding to the date and time passed
in ptm parameter.
On error, a -1 value is returned.
Portability.
Defined in ANSI-C.
Example.
/* mktime example: weekday calculator */
#include <stdio.h>
#include <time.h>
main ()
{
time_t rawtime;
struct tm * timeinfo;
int year, month ,day;
char * weekday[] = { "Sunday", "Monday",
"Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
/* prompt user for date */
printf ("Enter year: "); scanf ("%d",&year);
printf ("Enter month: "); scanf ("%d",&month);
printf ("Enter day: "); scanf ("%d",&day);
/* get current timeinfo and modify it to user's choice */
time ( &rawtime );
timeinfo = localtime ( &rawtime );
timeinfo->tm_year = year - 1900;
timeinfo->tm_mon = month - 1;
timeinfo->tm_mday = day;
/* call mktime: timeinfo->tm_wday will be set */
mktime ( timeinfo );
printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);
return 0;
}
Output:
Enter year: 2000
Enter month: 5
Enter day: 20
That day is a Saturday.
See also.
asctime,
gmtime,
localtime,
time