How to get the epoch timestamp in C?

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

asked May 23, 2017 by dtivl (2,500 points)

1 Answer

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, "%u\n", (unsigned)time(NULL)); 

On 64-bit POSIX systems:

fprintf(stdout, "%lu\n", (unsigned long)time(NULL)); 
answered May 27, 2017 by dtivl (2,500 points)

Please log in or register to answer this question.

Related questions

1 answer
1 answer
asked May 29, 2017 by dtivl (2,500 points)
1 answer
asked May 27, 2014 by Eric Z Ma (44,280 points)
1 answer
asked Jan 21, 2013 by anonymous
1 answer
asked May 23, 2017 by dtivl (2,500 points)
1 answer
asked May 23, 2017 by dtivl (2,500 points)
Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...