Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

mktime — convert and normalize local time

mktime converts local time stored in struct tm into time_t, normalizing the fields on success.

Version: C23; repository compiler flag, production runtime not attested

Syntax

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

mktime from <time.h> accepts a pointer to a writable struct tm. It interprets local time, not UTC; time_t need not encode Unix seconds.

tm_year counts years since 1900; tm_mon counts months from 0. Fields may exceed normal ranges. Success normalizes the structure and computes the initially ignored tm_wday and tm_yday.

tm_isdst: positive means daylight saving, zero means none, negative requests detection. For skipped or repeated clock times during transitions, the choice is unspecified.

The result is time_t; unrepresentable time yields (time_t)-1. Normalization is not strict date validation.

Examples

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

int main(void)
{
    struct tm t = {0};
    t.tm_year = 2024 - 1900;
    t.tm_mon = 0;
    t.tm_mday = 32;
    t.tm_hour = 12;
    t.tm_isdst = -1;

    if (mktime(&t) == (time_t)-1) {
        puts("conversion failed");
        return 1;
    }

    printf("%04d-%02d-%02d\n",
           t.tm_year + 1900, t.tm_mon + 1, t.tm_mday);
    return 0;
}

Result

2024-02-01

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources