mktime(), mktime64() — Convert local time

Standards

Standards / Extensions C or C++ Dependencies

ISO C
POSIX.1
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3
Language Environment®

both  

Format

#include <time.h>

time_t mktime(struct tm *tmptr);
#define _LARGE_TIME_API
#include <time.h>

time64_t mktime64 (struct tm *tmptr);
Compile requirement: Use of mktime64() function requires the long long data type. For more information on how to make long long data type available, see z/OS XL C/C++ Language Reference.

General description

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.

Returned value

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).

Error Code
Description
EOVERFLOW
The result cannot be represented.

Usage notes

  1. The ctime(), localtime(), and mktime() functions now return Coordinated Universal Time (UTC) unless customized locale information is made available, which includes setting the timezone_name variable.
  2. Applications working with local time may define time zone information by using the TZ (POSIX) or _TZ (non-POSIX) environment variable or by customizing the LC_TOD category of the locale in use.
  3. When neither TZ nor _TZ is defined, the current locale is interrogated for time zone information. If neither TZ nor _TZ is defined and LC_TOD time zone information is not present in the current locale, a default value is applied to local time. POSIX programs simply default to Coordinated Universal Time (UTC), while non-POSIX programs establish an offset from UTC based on the setting of the system clock. For more information about customizing a time zone to work with local time, see “Customizing a time zone” in z/OS XL C/C++ Programming Guide.
  4. The mktime() functions fails when a result overflows the time_t object used to return the number of seconds elapsed from the time in tmptr back to the start of the standard epoch. In 31-bit, the last year that mktime() supports is 2037. In 64-bit, the time_t grows from 4 bytes to 8 bytes in length, so that mktime() can accommodate dates further into the future. The upper bound in 64-bit is set to the year 9999.

Example

CELEBM19
⁄* 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]);                                                 
}                                                                               
Output
40 days and 16 hours from now, it will be a Sunday

Related information