C gmtime() Function

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

Definition

The C gmtime() function converts a time_t calendar value into a broken-down UTC time stored in struct tm. UTC does not apply the machine's local time-zone offset.

The returned pointer usually refers to shared static storage that a later gmtime(), localtime(), or related call may overwrite. Check for NULL before reading fields. Include <time.h>.

Syntax

Start with this concrete gmtime() call:

C
gmtime(&stamp);
CallWhat it does
gmtime(&stamp);Converts stamp to UTC fields in a library-owned struct tm.

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
struct tm *gmtime(const time_t *timer);

Parameters

ParameterDescription
timerPointer to the time_t calendar-time value to convert.

Return Value

Return ValueDescription
struct tm *Pointer to broken-down UTC fields in static storage.
NULLThe value cannot be represented as UTC broken-down time.

C Examples

Example 1 — Read UTC Fields

This example demonstrates gmtime() 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 stamp = (time_t)0;
    struct tm *utc = gmtime(&stamp);
    if (utc != NULL) {
        printf("%04d-%02d-%02d %02d:%02d:%02d UTC\n",
               utc->tm_year + 1900, utc->tm_mon + 1, utc->tm_mday,
               utc->tm_hour, utc->tm_min, utc->tm_sec);
    }
    return 0;
}

How It Works

  1. Step 1. Pass the address of a sample calendar value.
  2. Step 2. gmtime() fills standard struct tm fields for UTC.
  3. Step 3. Add 1900 to tm_year and 1 to zero-based tm_mon.
  4. Step 4. Print the remaining day and clock fields with leading zeros.

Example 2 — Format UTC with strftime

This example demonstrates gmtime() 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 *utc = gmtime(&now);
    char text[32];
    if (utc != NULL && strftime(text, sizeof text, "%Y-%m-%d %H:%M UTC", utc) > 0)
        printf("%s\n", text);
    return 0;
}

How It Works

  1. Step 1. Read the current calendar time.
  2. Step 2. Convert the same instant to UTC broken-down fields.
  3. Step 3. Give strftime() a destination and explicit format.
  4. Step 4. Print only after both conversion and formatting succeed.

Example 3 — Copy Before Another Conversion

This example demonstrates gmtime() 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 stamp = (time_t)0;
    struct tm *ptr = gmtime(&stamp);
    if (ptr != NULL) {
        struct tm saved = *ptr;
        (void)localtime(&stamp);
        printf("saved UTC hour: %d\n", saved.tm_hour);
    }
    return 0;
}

How It Works

  1. Step 1. Get the pointer to static UTC fields.
  2. Step 2. Copy the entire structure by value into saved.
  3. Step 3. Call another converter that may reuse the static buffer.
  4. Step 4. Read the independent copy safely after that call.

Frequently Asked Questions

gmtime() breaks a calendar time into UTC date and clock fields.
Include <time.h> before calling gmtime().
On success it returns a pointer to a static struct tm with UTC fields. On failure it returns NULL.
The static result is not thread-safe and can be overwritten. Use POSIX gmtime_r() when a caller-owned result is required.
It is declared by the C time library; exact representations, locale, time-zone data, and resolution can still vary by implementation.

Next: gmtime_r()

Continue through the C time.h function tutorials.

gmtime_r() →

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