Expand description
Two monotonic clocks read together, so an application can tell time spent working from time the machine spent asleep.
A wall clock cannot answer this: a time-synchronisation step moves it while nobody slept, and on the platforms where the monotonic clock keeps running through a suspend the wall clock moves by exactly the same amount, so their difference is zero. The answer is the difference between two monotonic clocks, one that stops while the machine sleeps and one that does not.
| Platform | Stops while asleep | Keeps counting |
|---|---|---|
| Linux, Android | CLOCK_MONOTONIC | CLOCK_BOOTTIME |
Only that pair is read here. On every other platform there is one clock, Uptime::elapsed
equals Uptime::awake, Uptime::suspended_since is always zero and
Uptime::detects_suspend returns false, so an application can say “I cannot tell sleep
apart on this system” instead of showing a number that is made up:
- macOS has
CLOCK_UPTIME_RAWandCLOCK_MONOTONIC_RAW, the pair that would answer this, but reading them needs a foreign function call, and the framework forbidsunsafe. The dependency that provides the clocks safely (rustix) exposes no Apple-only clock id, so the pair cannot be read from safe code today. - Windows has
QueryUnbiasedInterruptTimeandQueryInterruptTime, which again need a foreign function call, andrustixis Unix only. There,awakeis measured from the first reading in the process rather than from boot.
Structs§
- Uptime
- A moment read on both monotonic clocks at once.