Namespaces
Variants
Views
Actions

mktime

From cppreference.com
< c‎ | chrono
Revision as of 02:47, 9 October 2013 by Eendy (Talk | contribs)

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

Parameters

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

Return value

time since epoch as a time_t object on success or -1 if time cannot be represented as a time_t object.

Example

Display the time 100 months ago

#include <stdio.h>
#include <time.h>
 
int main()
{
    time_t t = time(NULL);
    struct tm *tmptr = localtime(&t);
    printf("Today is           %s", asctime(tmptr));
    tmptr->tm_mon -= 100;
    mktime(tmptr);
    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

See also

converts time since epoch to calendar time expressed as local time
(function) [edit]