You already use it: std::time(0) (don't forget #include <ctime> ). However, does std::time really return time, since the epoch is not specified in the standard ( C11 , which the C ++ standard refers to):
7.27.2.4 time function
abstract
#include <time.h> time_t time(time_t *timer);
Description
The time function determines the current calendar time. The value encoding is not specified. [Emphasis mine]
For C ++, C ++ 11 and later time_since_epoch . However, only in C ++ 20 and later versions of std::chrono::system_clock was Unix Time set and it is undefined and therefore possibly std::chrono::system_clock in previous standards.
However, on Linux std::chrono::system_clock will usually use Unix Time even in C ++ 11, C ++ 14 and C ++ 17, so you can use the following code:
source share