Home C time.h Functions gmtime_r() C gmtime_r() Function 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:
Call What 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 struct tm *gmtime_r(const time_t *restrict timer, struct tm *restrict result); Parameters Parameter Description timer Pointer to the calendar-time value to convert. result Pointer to caller-owned struct tm storage.
Return Value Return Value Description resultConversion succeeded; the same destination pointer is returned. NULLConversion failed.
C Examples 1. Convert Into Local Storage 2. Keep Two UTC Results 3. Format Reentrant UTC Example 1 — Convert Into Local Storage This example demonstrates gmtime_r() with a small, complete program and checks the relevant result before using it.
#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;
} UTC year: 1970
(on systems using the Unix epoch) How It Works Step 1. Allocate a struct tm in the caller.Step 2. Pass both the source pointer and destination pointer.Step 3. Check the returned pointer for conversion success.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.
#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;
} days: 1 then 2
(on systems using the Unix epoch) How It Works Step 1. Create two calendar values one sample day apart.Step 2. Provide a different destination for each conversion.Step 3. Neither call overwrites the other structure.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.
#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;
} 2026-11-17T14:45:00Z
(example value varies) How It Works Step 1. Obtain the current calendar value and verify it.Step 2. Write UTC fields into a stack-owned structure.Step 3. Format those fields into an ISO-style caller-owned character buffer.Step 4. Print only after every stage reports success.Frequently Asked Questions What does gmtime_r() do in C? gmtime_r() converts calendar time to UTC in a caller-provided structure.
Which header declares gmtime_r()? Include <time.h> before calling gmtime_r().
What does gmtime_r() return? On success it returns the destination pointer after filling your struct tm. On failure it returns NULL.
What should beginners check when using gmtime_r()? gmtime_r() is a POSIX API. Windows commonly provides gmtime_s() with a different parameter order and return type.
Is gmtime_r() portable? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion