Debian Codegraph @

third_party/rust/zeitstempel/src/fallback.rs

raw
1 use std::convert::TryInto;
2 use std::time::Instant;
3
4 use once_cell::sync::Lazy;
5
6 static INIT_TIME: Lazy<Instant> = Lazy::new(Instant::now);
7
8 pub fn now_including_suspend() -> u64 {
9 // For Windows:
10 // Instead of relying on figuring out the underlying functions,
11 // we can rely on the fact that `Instant::now` maps to [QueryPerformanceCounter] on Windows,
12 // so by comparing it to another arbitrary timestamp we will get a duration that will include
13 // suspend time.
14 // If we use that as a timestamp we can compare later timestamps to it and that will also
15 // include suspend time.
16 //
17 // [QueryPerformanceCounter]: https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter
18 //
19 // For other operating systems:
20 // This fallback is not used on Linux, where it maps to `CLOCK_MONOTONIC`, which does NOT
21 // include suspend time. But we don't use it there, so no problem.
22 //
23 // This fallback is not used on macOS, where it maps to `mach_absolute_time`, which does NOT
24 // include suspend time. But we don't use it there, so no problem.
25 //
26 // For other operating systems we make no guarantees, other than that we won't panic.
27 let now = Instant::now();
28 now.checked_duration_since(*INIT_TIME)
29 .and_then(|diff| diff.as_nanos().try_into().ok())
30 .unwrap_or(0)
31 }