Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C |
both |
#include <time.h>
time_t mktime(struct tm *tmptr);
#define _LARGE_TIME_API
#include <time.h>
time64_t mktime64 (struct tm *tmptr);
The mktime() function converts broken-down time, expressed as a local time, in the tm structure (described in Table 1) pointed to by tmptr, to calendar time. Calendar time is the number of seconds since epoch, which was at 00:00:00 Coordinated Universal Time (UTC), January 1, 1970.
The values of the structure members passed to the mktime() function are not restricted to the ranges described in time.h. For values that are outside the specified range, the mktime() function will use date arithmetic to adjust the values to produce a valid date and time (see Example). The values of tm_wday and tm_yday are ignored and are assigned their correct values on return.
On the successful completion of the function, all the members of the structure pointed to by time are set to represent the specified time since the Epoch with their values forced into the ranges described in time.h. The value of tm_wday is set after the values of tm_mon and tm_year are determined. The output value of tm_isdst is determined by the mktime() function, independent of the value presented as input to the mktime() function.
If an application uses the localtime() function to determine local time, the localtime() function will determine if daylight savings applies (assuming DST information is available) and will correctly set the tm_isdst flag. If the application wants to determine seconds from Epoch corresponding to a tm structure returned by the localtime() function, it should not modify the tm_isdst flag set by the localtime() function.
If an application sets tm_isdst to 0 before calling the mktime() function, it is asserting that daylight savings does not apply to the input values of the tm structure, regardless of the system DST start and end dates. Likewise, if the application has set a value for tm_isdst to be greater than 0, it is asserting that the time represented by the tm structure has been shifted for daylight savings. Therefore, the mktime() function unshifts the time in determining seconds since Epoch.
Setting tm_isdst to -1 indicates the mktime() function will determine whether daylight savings time applies. If yes, the mktime() function returns tm_isdst greater than 0. If not, it returns tm_isdst of 0 unless DST information is not available on the system, in which case tm_isdst of -1 is returned.
Your time zone might not be using DST, perhaps because the TZ environment variable does not specify a daylight savings time name or perhaps because DSTNAME is unspecified in the current LC_TOD locale category. In such a case, if you code tm_isdst=1 and call the mktime() function, (time-t)-1 is returned to indicate an error.
The mktime64() function behaves exactly like the mktime() function except it will support a structured date beyond 00:00:00 UTC January 1, 2038 with a limit of 23:59:59 UTC on December 31, 9999.
If successful, the mktime() function returns a time_t representing seconds since the Epoch, The number of seconds corresponds to the broken-down time, expressed as local time, based on the user-supplied tm structure pointed to by tmptr.
If the mktime() function cannot convert the broken-down time to a calendar time, it returns (time_t)-1 to indicate an error, such as time before January 1, 1970 (UTC).
⁄* CELEBM19
This example prints the day of the week that is 40 days and
16 hours from the current date.
*⁄
#include <stdio.h>
#include <time.h>
char *wday[] = { "Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday" };
int main(void)
{
time_t t1, t3;
struct tm *t2;
t1 = time(NULL);
t2 = localtime(&t1);
t2 -> tm_mday += 40;
t2 -> tm_hour += 16;
t3 = mktime(t2);
printf("40 days and 16 hours from now, it will be a %s \n",
wday[t2 -> tm_wday]);
}
40 days and 16 hours from now, it will be a Sunday