user513951
user513951

Reputation: 13690

Does the value of std::chrono::system_clock.now().time_since_epoch().count() increase monotonically?

I'm aware of steady_clock and that it is the only clock that is specified to be monotonic. And I understand that system_clock can jump forward or backward due to daylight savings and leap years. But doesn't count() give you the number of ticks of the system clock since the Unix epoch, which is an always increasing number, regardless of how that integer number of ticks is parsed into a "calendar date + wall clock" interpretation? i.e. Even if the "calendar date + wall clock" jumps from 2am from 3am on a given day in March, hasn't the integer count of ticks only increased by one tick?

In short, doesn't it stand to reason that the value of std::chrono::system_clock.now().time_since_epoch().count() should be expected to, in the short term, increase monotonically (barring updates to the system clock, which of course are a very real event), even if the date+time it refers to jumps around?

EDIT

As pointed out by @SergeyA, if the system clock is changed, then of course the value will jump around. But I think the change in wall clock time due to daylight savings is not an NTP update event or a manual change by a user. If it helps clarify the question, I'm interested in uptimes of an hour or two, which could cross the DST boundary, as opposed to uptimes of weeks or months, in which the clock could drift.

Upvotes: 5

Views: 3277

Answers (2)

Howard Hinnant
Howard Hinnant

Reputation: 219185

system_clock tracks Unix Time. Unix Time has no UTC offset adjustments (daylight saving). It is just a linear count of non-leap seconds. It is possible that an implementation could jump backwards during a leap second insertion. Though in practice the leap second is "smeared" with many tiny adjustments over a span of hours.

In theory it is possible for a system_clock to be monotonic. In practice, no clock keeps perfect time, and must be adjusted, (potentially backwards) to stay in sync with Unix Time.

In C++11/14/17, the Unix Time measure is not specified for system_clock, but it is the existing practice. In C++20, it will be specified.

Upvotes: 2

SergeyA
SergeyA

Reputation: 62603

Short answer - no, it does not. System clock can be (and will be in practice!) adjusted outside, as a result of manual action or source (NTP, PTP) synchronization.

Upvotes: 3

Related Questions