Get current timestamp in microseconds since epoch?

19,859

Solution 1

The epochs of the chrono clocks are unspecified. But practically you can think of the chrono clocks this way:

  1. 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.

  2. 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.

  3. high_resolution_clock is sometimes a type alias for steady_clock and sometimes a type alias for system_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();
Share:
19,859
user1950349
Author by

user1950349

Updated on July 22, 2022

Comments

  • user1950349
    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 use system_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
      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.
      Alex Lop. over 8 years
      @πάνταῥεῖ uint64_t ...
    • πάντα ῥεῖ
      πάντα ῥεῖ over 8 years
      Sorry guys confused with nanoseconds precision for the moment.
  • user1950349
    user1950349 over 8 years
    Thanks for great suggestion. Will we see any issues in leap years if we use steady_clock?
  • Howard Hinnant
    Howard Hinnant over 8 years
    Did you mean system_clock?!
  • Bo Persson
    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
    Howard Hinnant over 8 years
    But 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
    user1950349 over 8 years
    Yes I mean system_clock sorry for the confusion.
  • Howard Hinnant
    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
    Captain Giraffe over 8 years
    @HowardHinnant I really appreciate you shared this code. It is the first time I see it mentioned. Thanks.