mktime

From cppreference.com
< c‎ | chrono
Defined in header <time.h>
time_t mktime( struct tm *time );

Renormalizes local calendar time expressed as a struct tm object and also converts it to time since epoch as a time_t object. time->tm_wday and time->tm_yday are ignored. The values in time are not checked for being out of range.

A negative value of time->tm_isdst causes mktime to attempt to determine if Daylight Saving Time was in effect in the specified time.

If the conversion to time_t is successful, the time object is modified. All fields of time are updated to fit their proper ranges. time->tm_wday and time->tm_yday are recalculated using information available in other fields.

Contents

[edit] Parameters

time - pointer to a tm object specifying local calendar time to convert

[edit] Return value

time since epoch as a time_t object on success or -1 if time cannot be represented as a time_t object (POSIX also requires EOVERFLOW to be stored in errno in this case).

[edit] Notes

If the struct tm object was obtained from POSIX strptime or equivalent function, the value of tm_isdst is indeterminate, and needs to be set explicitly before calling mktime.

[edit] Example

#include <stdio.h>
#include <time.h>
 
int main(void)
{
    struct tm tm = *localtime(&(time_t){time(NULL)});
    printf("Today is           %s", asctime(&tm));
    tm.tm_mon -= 100;  // tm_mon is now outside its normal range
    mktime(&tm); // recalculate tm
    printf("100 months ago was %s", asctime(&tm));
}

Output:

Today is           Tue Feb 17 13:46:01 2015
100 months ago was Tue Oct 17 14:46:01 2006

[edit] References

  • C11 standard (ISO/IEC 9899:2011):
  • 7.27.2.3 The mktime function (p: 390-391)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.23.2.3 The mktime function (p: 340-341)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 4.12.2.3 The mktime function

[edit] See also

converts time since epoch to calendar time expressed as local time
(function)
C++ documentation for mktime