Difference between steady_clock vs system_clock?

前端 未结 5 1024
情深已故
情深已故 2020-12-07 16:40

I am trying to see whether my data is 120 second old or not by looking at the timestamp of the data so I have below code:

uint64_t now = duration_cast

        
5条回答
  •  抹茶落季
    2020-12-07 17:42

    First things first

    The reason you're seeing a positive value is due to the unsigned integer wrap-around. Try this and see:

    std::cout << static_cast  (-1) << std::endl;
    

    Is the value returned by getTimestamp() expected? If not, it's a little hard to see what's wrong, without seeing the implementation of getTimestamp(). It looks like the timestamp was not measured using the same clock.

    Steady vs. System Time

    The steady clock is best for measuring time intervals. To quote from cppreference.com:

    Class std::chrono::steady_clock represents a monotonic clock. The time points of this clock cannot decrease as physical time moves forward. This clock is not related to wall clock time, and is best suitable for measuring intervals.

    As opposed to the system_clock, which is not monotonic (i.e. the time can decrease if, say, the user changes the time on the host machine.)

提交回复
热议问题