C Unix Timestamp Snippets
C examples for getting the current Unix timestamp, converting epoch seconds to a human-readable date with strftime(), and building timestamps from calendar dates using timegm().
#include <time.h>
time_t ts = time(NULL);#include <time.h>
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
long ms = (long)ts.tv_sec * 1000L + ts.tv_nsec / 1000000L;#include <time.h>
#include <stdio.h>
time_t ts = 1700000000;
struct tm *tm_info = gmtime(&ts); // UTC
char buf[64];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm_info);
printf("%s\n", buf); // "2023-11-15 06:13:20"#include <time.h>
time_t ts = 1700000000;
struct tm *tm_info = localtime(&ts); // host timezone
char buf[64];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm_info);#define _GNU_SOURCE
#include <time.h>
struct tm t = {0};
t.tm_year = 2023 - 1900; // years since 1900
t.tm_mon = 10; // 0-indexed; 10 = November
t.tm_mday = 15;
t.tm_hour = 6;
t.tm_min = 13;
t.tm_sec = 20;
time_t ts = timegm(&t); // GNU/POSIX; use mktime() for local#include <time.h>
time_t start = time(NULL);
/* ... work ... */
time_t end = time(NULL);
double elapsed = difftime(end, start);C timestamp basics
C represents Unix time as time_t, a signed integer type defined in <time.h>. On 64-bit systems time_t is 64 bits and immune to the Year 2038 overflow. On 32-bit systems it is still 32 bits on many platforms; check sizeof(time_t) to confirm. The standard function time(NULL) returns the current Unix time in seconds.
gmtime vs localtime
gmtime() converts a time_t to a struct tm in UTC. localtime() converts to the host system's local timezone. Always use gmtime() when you need a reproducible, timezone-independent result. Both functions return a pointer to a shared static struct; copy the result with a struct tm local variable if you need it across function calls.
C production notes
Prefer clock_gettime(CLOCK_REALTIME) over gettimeofday() for sub-second precision; POSIX marks gettimeofday() as obsolescent. Use timegm() (GNU/BSD) or a portable equivalent when converting calendar dates to UTC timestamps; mktime() interprets its input as local time, which silently produces wrong results in non-UTC environments.
- Check sizeof(time_t) on embedded targets; 32-bit time_t wraps in January 2038
- Use gmtime_r() and localtime_r() in multi-threaded code to avoid data races
- strftime() format strings are locale-independent; prefer %Y-%m-%d over %x for interoperability
- timegm() is not in POSIX; use _mkgmtime() on Windows or implement manually with mktime() + UTC offset
Convert Linux time: date(1), time_t, gmtime, strftime
Searches for "convert linux time" or Linux timestamp conversion usually mean the shell command date(1) or the C library time_t functions below. Linux stores the Unix timestamp as time_t — signed 32-bit on legacy systems (Y2038 limit), signed 64-bit on modern.
- Shell (GNU date): date -u -d @1700000000 — epoch → UTC datetime
- Shell (BSD/macOS): date -ur 1700000000 — note -r instead of -d @
- Shell current epoch: date +%s (seconds), date +%s%3N (ms, GNU only)
- C library: time_t t = time(NULL); struct tm *u = gmtime(&t); strftime(buf, sizeof(buf), '%Y-%m-%dT%H:%M:%SZ', u);
- C library: timegm(struct tm *) returns time_t for a UTC calendar time
- POSIX time_t: signed 32-bit overflows on 2038-01-19; 64-bit time_t is the modern fix
Linux epoch time, linux timestamp to time, online Linux converter
Variants: "linux epoch time" (Unix time on Linux), "linux timestamp to time" (epoch → wall-clock), "linux timestamp converter online" (this page).
- Linux epoch time / linux epoch time — same as Unix time; current value: date +%s
- Linux timestamp to time / linux timestamp to time — date -u -d @1700000000 returns the UTC wall-clock time
- Linux timestamp converter online / linux timestamp converter online — this page is the browser-based equivalent of date(1)
FAQ
- What type should I use for Unix timestamps in C?
- Use time_t for second precision and int64_t (from <stdint.h>) when you need to store milliseconds or nanoseconds. Avoid int or long because their sizes are platform-dependent.
- How do I get milliseconds in C?
- Use clock_gettime(CLOCK_REALTIME, &ts) from <time.h>. The result is in ts.tv_sec (seconds) and ts.tv_nsec (nanoseconds). Compute milliseconds as ts.tv_sec * 1000LL + ts.tv_nsec / 1000000.