time_t and the Unix Epoch
In a POSIX environment, time_t represents time in seconds since the Epoch. The C language alone does not guarantee this representation on every platform. Do not assume that time_t is always 32 bits either.
Display a timestamp in UTC
#include <stdio.h>
#include <time.h>
int main(void) {
time_t timestamp = (time_t)1711972800;
struct tm *utc = gmtime(×tamp);
char output[32];
if (utc == NULL) return 1;
if (strftime(output, sizeof output,
"%Y-%m-%dT%H:%M:%SZ", utc) == 0) return 1;
puts(output); /* 2024-04-01T12:00:00Z */
return 0;
}Calculate a difference
difftime(end, start) calculates a difference in seconds without directly assuming the underlying arithmetic type of time_t.
time_t start = (time_t)1711972800;
time_t end = (time_t)1711981800;
double elapsed = difftime(end, start);
/* elapsed = 9000.0 */What to check
gmtimedisplays UTC;localtimeuses the local time zone.- Legacy functions may use shared memory. In a concurrent POSIX program, consider
gmtime_randlocaltime_r. mktimeinterprets a local date. Do not use it as an implicit UTC converter.- To measure execution time on POSIX, use
clock_gettime(CLOCK_MONOTONIC, ...).
Check the size and capabilities of the library for dates beyond 2038. The processor type alone does not determine every format being handled.
References
English version published September 26, 2026.