How to get the epoch timestamp in C?

In C, how to get the epoch timestamp, the number of seconds passed since the epoch?

In C, from man 7 time:

UNIX systems represent time in seconds since the Epoch, 1970-01-01
00:00:00 +0000 (UTC).

A program can determine the calendar time using gettimeofday(2), which
returns time (in seconds and microseconds) that have elapsed since the
Epoch; time(2) provides similar information, but only with accuracy to
the nearest second.

You can use the time() library function to get the epoch timestamp:

On 32-bit POSIX systems:

fprintf(stdout, "%un", (unsigned)time(NULL)); 

On 64-bit POSIX systems:

fprintf(stdout, "%lun", (unsigned long)time(NULL)); 

One comment:

Leave a Reply

Your email address will not be published. Required fields are marked *