C gmtime_r() Function

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

Definition

The POSIX gmtime_r() function converts a time_t value into UTC fields, like gmtime(), but writes them into the struct tm supplied by the caller.

Because each caller owns its result object, the reentrant form avoids the shared static buffer used by gmtime(). It is POSIX rather than ISO C, so check target-platform availability. Include <time.h>.

Syntax

Start with this concrete gmtime_r() call:

C
gmtime_r(&stamp, &utc);
CallWhat it does
gmtime_r(&stamp, &utc);Writes UTC fields for stamp into utc.

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_r(const time_t *restrict timer, struct tm *restrict result);

Parameters

ParameterDescription
timerPointer to the calendar-time value to convert.
resultPointer to caller-owned struct tm storage.

Return Value

Return ValueDescription
resultConversion succeeded; the same destination pointer is returned.
NULLConversion failed.

C Examples

Example 1 — Convert Into Local Storage

This example demonstrates gmtime_r() 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;
    if (gmtime_r(&stamp, &utc) != NULL)
        printf("UTC year: %d\n", utc.tm_year + 1900);
    return 0;
}

How It Works

  1. Step 1. Allocate a struct tm in the caller.
  2. Step 2. Pass both the source pointer and destination pointer.
  3. Step 3. Check the returned pointer for conversion success.
  4. Step 4. Read the caller-owned year and translate it from years since 1900.

Example 2 — Keep Two UTC Results

This example demonstrates gmtime_r() 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 a = (time_t)0, b = (time_t)86400;
    struct tm first, second;
    if (gmtime_r(&a, &first) && gmtime_r(&b, &second))
        printf("days: %d then %d\n", first.tm_mday, second.tm_mday);
    return 0;
}

How It Works

  1. Step 1. Create two calendar values one sample day apart.
  2. Step 2. Provide a different destination for each conversion.
  3. Step 3. Neither call overwrites the other structure.
  4. Step 4. Read both day fields together after both calls complete.

Example 3 — Format Reentrant UTC

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

How It Works

  1. Step 1. Obtain the current calendar value and verify it.
  2. Step 2. Write UTC fields into a stack-owned structure.
  3. Step 3. Format those fields into an ISO-style caller-owned character buffer.
  4. Step 4. Print only after every stage reports success.

Frequently Asked Questions

gmtime_r() converts calendar time to UTC in a caller-provided structure.
Include <time.h> before calling gmtime_r().
On success it returns the destination pointer after filling your struct tm. On failure it returns NULL.
gmtime_r() is a POSIX API. Windows commonly provides gmtime_s() with a different parameter order and return type.
It is specified by POSIX rather than ISO C, so check the target platform.

Next: localtime()

Continue through the C time.h function tutorials.

localtime() →

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