C timegm() Function

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

Definition

The commonly available timegm() function interprets a struct tm as UTC and converts it to time_t. Unlike mktime(), it does not interpret the fields as local time.

It is standardized by POSIX.1-2024 and is also a long-standing extension on many systems, but older C/POSIX environments may not declare it. The function normalizes fields and modifies the supplied structure. Include <time.h>.

Syntax

Start with this concrete timegm() call:

C
timegm(&utc);
CallWhat it does
timegm(&utc);Interprets and normalizes utc as UTC, then returns calendar time.

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 timegm(struct tm *time_ptr);

Parameters

ParameterDescription
time_ptrPointer to a writable struct tm interpreted as UTC.

Return Value

Return ValueDescription
time_t valueCalendar-time representation of the normalized UTC fields.
(time_t)-1The UTC value cannot be represented.

C Examples

Example 1 — Build a UTC Instant

This example demonstrates timegm() 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 utc = {0};
    utc.tm_year = 100;
    utc.tm_mon = 0;
    utc.tm_mday = 1;
    time_t stamp = timegm(&utc);
    printf("conversion: %s\n", stamp == (time_t)-1 ? "failed" : "ok");
    return 0;
}

How It Works

  1. Step 1. Zero-initialize the UTC fields.
  2. Step 2. Encode 2000 as year 100 and January as month 0.
  3. Step 3. timegm() interprets the structure without a local-zone offset.
  4. Step 4. Check the returned calendar value for the failure sentinel.

Example 2 — Normalize UTC Fields

This example demonstrates timegm() 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 utc = {0};
    utc.tm_year = 124;
    utc.tm_mon = 12;
    utc.tm_mday = 1;
    if (timegm(&utc) != (time_t)-1)
        printf("%04d-%02d-%02d\n", utc.tm_year+1900, utc.tm_mon+1, utc.tm_mday);
    return 0;
}

How It Works

  1. Step 1. Use month 12, one past the normal zero-based range.
  2. Step 2. Treat all fields as UTC rather than local time.
  3. Step 3. Conversion carries the extra month into the next year.
  4. Step 4. Read the normalized fields written back by timegm().

Example 3 — Round Trip Through UTC

This example demonstrates timegm() 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 original = (time_t)0;
    struct tm *ptr = gmtime(&original);
    if (ptr != NULL) {
        struct tm utc = *ptr;
        time_t again = timegm(&utc);
        printf("round trip: %s\n", again == original ? "yes" : "no");
    }
    return 0;
}

How It Works

  1. Step 1. Break a calendar value into UTC fields.
  2. Step 2. Copy the static gmtime() result into writable storage.
  3. Step 3. timegm() converts those UTC fields back.
  4. Step 4. The comparison confirms the two operations describe the same representable instant.

Frequently Asked Questions

timegm() converts UTC broken-down time to calendar time.
Include <time.h> before calling timegm().
On success it returns the time_t for the UTC fields. On failure it returns (time_t)-1. timegm() is a common extension, not ISO C.
Check availability and feature-test requirements on the target platform. Do not emulate timegm() by temporarily changing the process time zone in multi-threaded code.
It is standardized by recent POSIX and common as an extension, but older platforms may not provide it.

Next: timespec_get()

Continue through the C time.h function tutorials.

timespec_get() →

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