timestampinfo.fr
Tools & documentation

TIME DOCUMENTATION

Working with timestamps in C

Read the clock, display a UTC date and calculate an interval in a POSIX environment.

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(&timestamp);
    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

  • gmtime displays UTC; localtime uses the local time zone.
  • Legacy functions may use shared memory. In a concurrent POSIX program, consider gmtime_r and localtime_r.
  • mktime interprets 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

Reference documentation

Open the converter

English version published September 26, 2026.