mktime

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

Converts local calendar time to a 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.

If the conversion 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.

[edit] Notes

If the 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

Display the time 100 months ago

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
 
int main(void)
{
    time_t t = time(NULL);
    struct tm *tmptr = localtime(&t);
    printf("Today is           %s", asctime(tmptr));
    tmptr->tm_mon -= 100;
    time_t result = mktime(tmptr);
    if (result == ((time_t)-1))
    {
       fprintf(stderr,"mktime() failed in file %s at line # %d\n", __FILE__,__LINE__-3);
       exit(EXIT_FAILURE);
    }
    printf("100 months ago was %s", asctime(tmptr));
}

Output:

Today is           Wed Oct  9 10:47:25 2013
100 months ago was Thu Jun  9 10:47:25 2005

[edit] See also

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