Get current timestamp in microseconds since epoch?
Solution 1
The epochs of the chrono clocks are unspecified. But practically you can think of the chrono clocks this way:
The epoch of
steady_clock
is the time your application launched plus a signed random offset. I.e. you can't depend upon the epoch being the same across application launches. But the epoch will remain stable while an application is running.The epoch of
system_clock
is time since New Years 1970, not counting leap seconds, in the UTC timezone. Different implementations implement this with varying precision: libc++ counts microseconds, VS counts 1/10 of microseconds, and gcc counts nanoseconds.high_resolution_clock
is sometimes a type alias forsteady_clock
and sometimes a type alias forsystem_clock
.
For a time stamp in microseconds I recommend first defining this type alias:
using time_stamp = std::chrono::time_point<std::chrono::system_clock,
std::chrono::microseconds>;
Store that, instead of uint64_t
. The type safety of this type will save you countless run time errors. You'll discover your errors at compile time instead.
You can get the current time_stamp
with:
using namespace std::chrono;
time_stamp ts = time_point_cast<microseconds>(system_clock::now());
Solution 2
Another possibility for people who couldn't get other solutions to work:
uint64_t microseconds_since_epoch = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
user1950349
Updated on July 22, 2022Comments
-
user1950349 almost 2 years
I have a below code from which we are trying to get current timestamp in microseconds since epoch time but we are using
steady_clock
.inline uint64_t get_timestamp() { std::chrono::time_point<std::chrono::steady_clock> ts = std::chrono::steady_clock::now(); return std::chrono::duration_cast<std::chrono::microseconds>(ts.time_since_epoch()).count(); }
Is this the right way to do that since as per my understanding
steady_clock
is used to measure the passage of time not to get the current time of day? Or should I usesystem_clock
for this like as shown below:inline uint64_t get_timestamp() { std::chrono::time_point<std::chrono::system_clock> ts = std::chrono::system_clock::now(); return std::chrono::duration_cast<std::chrono::microseconds>(ts.time_since_epoch()).count(); }
I need to use
std::chrono
package only since that's what all our code is using.-
Nik Bougalis over 8 years@πάνταῥεῖ
std::uint64_t
? At least for a bit longer, anyways. Current time since epoch is 1440440480 seconds at the time of this writing, which is 0x51dac207a0000 microseconds. -
Alex Lop. over 8 years@πάνταῥεῖ
uint64_t
... -
πάντα ῥεῖ over 8 yearsSorry guys confused with nanoseconds precision for the moment.
-
-
user1950349 over 8 yearsThanks for great suggestion. Will we see any issues in leap years if we use
steady_clock
? -
Howard Hinnant over 8 yearsDid you mean
system_clock
?! -
Bo Persson over 8 years@user1950349 - The leap seconds are adjustments made to official time because the Earth's orbit is slightly wobbly (very slightly) so sometimes we arrive half a second early. Computers often ignore this, as it's hard enough anyway to calculate time.
-
Howard Hinnant over 8 yearsBut if you really want to play with leap seconds, here is a library + example on how to do it: howardhinnant.github.io/tz.html#flightexample2
-
user1950349 over 8 yearsYes I mean
system_clock
sorry for the confusion. -
Howard Hinnant over 8 years
system_clock
is counting Unix Time (en.wikipedia.org/wiki/Unix_time) which is nothing but a count of non-leap seconds since 1970 (UTC). And as coded above, actually that's a count of microseconds. It knows nothing of days, weeks, months, years or leap years. If you want to convert into those units, I recommend this date library: howardhinnant.github.io/date_v2.html (which will correctly handle leap years). -
Captain Giraffe over 8 years@HowardHinnant I really appreciate you shared this code. It is the first time I see it mentioned. Thanks.