[Previous]
[Contents]
[Next]

mktime()

convert local time to calendar time

Synopsis:

#include <time.h>
time_t mktime( struct tm *timeptr );

Description:

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

The original values of the fields tm_sec, tm_min, tm_hour, tm_mday and tm_mon aren't restricted to ranges described for struct tm. If these fields aren't in their proper ranges, they are 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:

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

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

Returns:

The converted calendar time.

Examples:

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

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

void main()
  {
    struct tm new_year;

    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;
    mktime( &new_year );
    printf( "The next century begins on a %s\n",
         week_day[ new_year.tm_wday ] );
  }

produces the output:

The next century begins on a Monday

Classification:

ANSI

Safety:
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

asctime(), clock(), ctime(), difftime(), gmtime(), localtime(), strftime(), time(), tzset()

The tm structure is described in the section on <time.h> of the Header Files chapter.


[Previous]
[Contents]
[Next]