Convert local time to calendar time


#include <time.h>

time_t mktime( struct tm* timeptr );


A pointer to a tm structure that contains the local time that you want to convert.



Use the -l c option to qcc to link against this library. This library is usually included automatically.


The mktime() function converts the local time information in the struct tm specified by timeptr into a calendar time (Coordinated Universal Time) with the same encoding used by the time() function.

The original values of the tm_sec, tm_min, tm_hour, tm_mday and tm_mon fields aren't restricted to the ranges described for struct tm. If these fields aren't in their proper ranges, they're adjusted so that they are. Values for the fields tm_wday and tm_yday are computed after all the other fields have been adjusted.

The original value of tm_isdst is interpreted as follows:

< 0
This field is computed as well.
Daylight savings time isn't in effect.
> 0
Daylight savings time is in effect.

Whenever mktime() is called, the tzset() function is also called.


The converted calendar time, or -1 if mktime() can't convert it.


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

static const char *week_day[] = {
    "Sunday", "Monday", "Tuesday", "Wednesday",
    "Thursday", "Friday", "Saturday"

int main( void )
    struct tm new_year;
    time_t t;

    new_year.tm_year  = 2001 - 1900;
    new_year.tm_mon   = 0;
    new_year.tm_mday  = 1;
    new_year.tm_hour  = 0;
    new_year.tm_min   = 0;
    new_year.tm_sec   = 0;
    new_year.tm_isdst = 0;

    t = mktime( &new_year );
    if ( t == (time_t)-1)
        printf("No conversion possible.\n");
        printf( "The 21st century began on a %s.\n",
            week_day[ new_year.tm_wday ] );
    return EXIT_SUCCESS;

produces the output:

The 21st century began on a Monday.


ANSI, POSIX 1003.1

Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

asctime(), asctime_r(), clock(), ctime(), ctime_r(), difftime(), gmtime(), gmtime_r(), localtime(), localtime_r(), strftime(), time(), tm, tzset()