Unix Timestamp in C
C's time() function has returned Unix timestamps since the 1970s — it's where the epoch convention originated. For sub-second precision, use clock_gettime() with CLOCK_REALTIME.
Code Examples
Current timestamp (seconds)
#include <time.h>
#include <stdio.h>
int main(void) {
time_t now = time(NULL);
printf("%ld\n", (long)now);
return 0;
}time(NULL) returns the current Unix timestamp as a time_t (typically a 64-bit signed integer on modern platforms). Cast to long for printf portability.
Current timestamp (milliseconds) — POSIX
#include <time.h>
#include <stdio.h>
int main(void) {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
long long ms = (long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
printf("%lld\n", ms);
return 0;
}clock_gettime with CLOCK_REALTIME gives nanosecond precision. tv_sec is full seconds; tv_nsec is the nanosecond remainder. Divide nanoseconds by 1,000,000 for milliseconds.
Convert timestamp to human-readable string
#include <time.h>
#include <stdio.h>
time_t ts = 1708560000;
struct tm *tm_info = gmtime(&ts); /* UTC */
char buf[64];
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", tm_info);
printf("%s\n", buf); /* 2024-02-22T00:00:00Z */gmtime converts a time_t to a struct tm in UTC. strftime formats it as a string. Use localtime instead of gmtime for the local timezone.
Thread-safe conversion — gmtime_r / localtime_r
#include <time.h> time_t ts = time(NULL); struct tm result; gmtime_r(&ts, &result); /* POSIX, thread-safe */ /* localtime_r(&ts, &result); for local timezone */
The reentrant variants gmtime_r and localtime_r are thread-safe — they write into a caller-provided struct tm instead of a static buffer. Prefer these in multi-threaded code.
Year 2038 check — ensure 64-bit time_t
#include <time.h>
#include <stdio.h>
int main(void) {
printf("sizeof(time_t) = %zu bytes\n", sizeof(time_t));
/* Should print 8 on 64-bit systems */
return 0;
}On 32-bit systems or old compilers, time_t may be 32-bit, causing overflow in January 2038. Modern 64-bit Linux/macOS systems use 64-bit time_t, safe until year 292 billion.
Note
C is where Unix timestamps originated — time() has been part of the C standard library since C89. On POSIX systems (Linux, macOS), clock_gettime() is the modern choice for high-resolution timestamps. On Windows, use GetSystemTimeAsFileTime() and convert from the Windows epoch (January 1, 1601). Always cast time_t to long or long long for printf — the underlying size is implementation-defined.
Frequently Asked Questions
How do I get the current Unix timestamp in C?
Call time(NULL) from <time.h>. It returns the current time as a time_t value — the number of seconds since January 1, 1970 UTC. For millisecond precision, use clock_gettime(CLOCK_REALTIME, &ts) from <time.h> on POSIX systems.
Is time_t 32-bit or 64-bit in C?
On modern 64-bit Linux and macOS systems, time_t is a 64-bit signed integer, safe until the year 292 billion. On older 32-bit systems, time_t may be 32-bit, which will overflow on January 19, 2038. You can check with: printf("%zu bytes\n", sizeof(time_t));
What is the Year 2038 problem in C?
32-bit systems store Unix timestamps in a signed 32-bit integer that overflows on January 19, 2038 at 03:14:07 UTC. All modern 64-bit Linux and macOS systems use 64-bit time_t and are not affected. Embedded systems and legacy 32-bit code may still be at risk.
How do I get millisecond timestamps in C?
Use clock_gettime(CLOCK_REALTIME, &ts). The result gives you ts.tv_sec (seconds) and ts.tv_nsec (nanoseconds). Convert to milliseconds with: long long ms = (long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
What is the difference between gmtime() and localtime() in C?
gmtime() converts a time_t to a struct tm in UTC. localtime() converts it to the local system timezone. For portable code that doesn't depend on the system's timezone setting, always prefer gmtime() or the thread-safe gmtime_r().
Main Epoch Converter
Use the homepage when you need epoch to date, date to epoch, or a full epoch converter
This C guide shows how to create and parse Unix timestamps in code. If your next step is broader search intent like epoch converter, epoch to date, epoch time to date, or unix epoch converter, jump back to the live homepage tools to paste a raw Unix value, auto-detect seconds versus milliseconds, and copy the readable answer instantly.