Determine durations with monotonic clocks if available
rachelbythebay.com
rachelbythebay.com
So you can't use absl if you need your time logic to be correct and also work in non-smearing environments, or in environments where the RTC isn't guaranteed to always be well-synced.
With a special API for folks who don't want that.
So everything works, unless you want to squeeze the last bit of performance from the machine, in which case you would have to do extra work.
Aka, correctness first, then comes performance.
I once rented a brand-new Linux server in a data center, and MySQL came up before the first time the clock was set. This confused things. But an ordinary reboot fixed it.
Well, maybe not:
> In November 2022 at the 27th General Conference on Weights and Measures, it was decided to abandon the leap second by or before 2035. From then the difference between atomic and astronomical time will be allowed to grow to a larger value yet to be determined.
I just hope that 2035 is early enough to prevent a negative leap second, which hasn't happened before. It would be quite a brave experiment to have one of those just before abandoning the whole concept.
In reality there is no absolute way to guarantee that a particular clock source is steady; virtualization can easily wreck them, so an external verification is the only definitive solution. You can say that macOS `CLOCK_MONOTONIC` is more steady than Linux `CLOCK_MONOTONIC`, but you can never assume they are actually steady all the time; they are only steady enough for not causing issues in typical cases.
All the regular Linux/Windows monotonic clocks are broken in some scenarios and will go back or just not increase.
On most systems CLOCK_MONOTONIC actually is monotonic. It's not necessarily steady or providing SI seconds, but its monotonic.
You'll need stuff like GPS if you want a monotonic, steady clock synchronized with other clocks.
An issue with monotonic clocks I had in the past is they usually had lower resolution that the default. I’d like to think this isn’t a problem anymore?
On Linux it can be argued that CLOCK_MONOTONIC_RAW should be used for durations. From the Linux man page:
CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific)
Similar to CLOCK_MONOTONIC, but provides access to
a raw hardware-based time that is not subject to
NTP adjustments or the incremental adjustments per‐
formed by adjtime(3). This clock does not count
time that the system is suspended.
There is also CLOCK_BOOTTIME to keep counting time while suspended: CLOCK_BOOTTIME (since Linux 2.6.39; Linux-specific)
A nonsettable system-wide clock that is identical
to CLOCK_MONOTONIC, except that it also includes
any time that the system is suspended. This allows
applications to get a suspend-aware monotonic clock
without having to deal with the complications of
CLOCK_REALTIME, which may have discontinuities if
the time is changed using settimeofday(2) or simi‐
lar.As I understanding it, when you're measuring real actual time you want the adjustments because otherwise you'll end up less accurate.
CLOCK_MONOTONIC_RAW is useful if you expect to use some other synchronization.
For those wondering: https://www.livescience.com/goodbye-leap-second-2035