C mktime() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<time.h>

Definition

The C mktime() function interprets a struct tm as local time and converts it to a time_t calendar value. It is the usual inverse of localtime().

The function also normalizes out-of-range fields and writes the normalized values back to the structure. Set tm_isdst to -1 when the library should determine daylight-saving status. Include <time.h>.

Syntax

Start with this concrete mktime() call:

C
mktime(&local);
CallWhat it does
mktime(&local);Interprets and normalizes local, then returns its calendar-time value.

Declare the required objects with the time types shown below, include <time.h>, and check the documented result before using output data.

Library declaration
C
time_t mktime(struct tm *time_ptr);

Parameters

ParameterDescription
time_ptrPointer to a writable struct tm interpreted as local time.

Return Value

Return ValueDescription
time_t valueCalendar-time representation of the normalized local date and time.
(time_t)-1The local time cannot be represented.

C Examples

Example 1 — Build a Local Date

This example demonstrates mktime() with a small, complete program and checks the relevant result before using it.

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

int main(void) {
    struct tm local = {0};
    local.tm_year = 124;
    local.tm_mon = 0;
    local.tm_mday = 7;
    local.tm_hour = 14;
    local.tm_isdst = -1;
    time_t stamp = mktime(&local);
    printf("conversion: %s\n", stamp == (time_t)-1 ? "failed" : "ok");
    return 0;
}

How It Works

  1. Step 1. Zero-initialize all fields.
  2. Step 2. Encode 2024 and January using struct tm conventions.
  3. Step 3. Let the library determine daylight-saving status.
  4. Step 4. mktime() converts the local date and reports failure with -1.

Example 2 — Normalize a Date

This example demonstrates mktime() with a small, complete program and checks the relevant result before using it.

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

int main(void) {
    struct tm local = {0};
    local.tm_year = 124;
    local.tm_mon = 0;
    local.tm_mday = 32;
    local.tm_isdst = -1;
    if (mktime(&local) != (time_t)-1)
        printf("%04d-%02d-%02d\n", local.tm_year+1900, local.tm_mon+1, local.tm_mday);
    return 0;
}

How It Works

  1. Step 1. Start with January day 32, an out-of-range field.
  2. Step 2. Tell mktime() to infer daylight-saving status.
  3. Step 3. Conversion carries the extra day into the next month.
  4. Step 4. Read the normalized fields that were written back.

Example 3 — Add One Day

This example demonstrates mktime() with a small, complete program and checks the relevant result before using it.

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

int main(void) {
    time_t now = time(NULL);
    struct tm *ptr = localtime(&now);
    if (ptr != NULL) {
        struct tm tomorrow = *ptr;
        tomorrow.tm_mday += 1;
        tomorrow.tm_isdst = -1;
        if (mktime(&tomorrow) != (time_t)-1)
            printf("tomorrow day: %d\n", tomorrow.tm_mday);
    }
    return 0;
}

How It Works

  1. Step 1. Convert the current instant to local calendar fields.
  2. Step 2. Copy shared storage before editing it.
  3. Step 3. Increment the day and request a fresh DST decision.
  4. Step 4. mktime() normalizes month or year rollover automatically.

Frequently Asked Questions

mktime() converts local broken-down time to calendar time and normalizes fields.
Include <time.h> before calling mktime().
On success it returns the matching time_t and normalizes the struct tm fields. On failure it returns (time_t)-1.
mktime() modifies the supplied structure. Initialize it fully enough for your use, especially tm_isdst.
It is declared by the C time library; exact representations, locale, time-zone data, and resolution can still vary by implementation.

Next: strftime()

Continue through the C time.h function tutorials.

strftime() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful