liveBook · Manning

Gmtime_r

As explained in Modern C, Third Edition

gmtime_r is a C time-conversion function that stores a time_t value as broken-down Coordinated Universal Time.

gmtime_r converts a time_t value into broken-down Coordinated Universal Time (UTC), storing the result in caller-provided storage. section 8.6

Store a UTC breakdown

time_t timestamp = time(NULL);\nstruct tm utc;\ngmtime_r(&timestamp, &utc);\n\nAfter the call, utc holds the broken-down UTC representation of timestamp. section 8.6

Keep these distinctions in mind:

  • Use localtime_r for broken-down local time; gmtime_r assumes UTC. section 8.6
  • Pair gmtime_r with timegm when converting between time_t and broken-down UTC. section 8.6
  • Prefer reentrant interfaces over traditional functions that operate on global state. section 8.6
time_t timegm(struct tm *timeptr);
struct tm *gmtime_r(const time_t *timer, struct tm *buf);
struct tm *localtime_r(const time_t *timer, struct tm *buf);

Compare the declarations of gmtime_r, localtime_r, and timegm. section 8.6

How Modern C, Third Edition covers it

How deep it goes
Defines the conversion and storage distinction, but does not detail struct tm fields or failure behavior.
Examples
Uses the gmtime_r declaration alongside localtime_r and timegm in a time-conversion function listing.
What it assumes
Assumes familiarity with time_t, struct tm, pointers, and basic C library function calls.
What you can do after
Apply it via the C time API to store a timestamp's broken-down UTC representation in supplied storage.
How it connects
Connects time_t, struct tm, localtime_r, timegm, and mktime through inverse time conversions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.