Debian Codegraph @

third_party/rust/zeitstempel/src/lib.rs

raw
1 //! zeitstempel is German for "timestamp".
2 //!
3 //! ---
4 //!
5 //! Time's hard. Correct time is near impossible.
6 //!
7 //! This crate has one purpose: give me a timestamp as an integer, coming from a monotonic clock
8 //! source, include time across suspend/hibernation of the host machine and let me compare it to
9 //! other timestamps.
10 //!
11 //! It becomes the developer's responsibility to only compare timestamps obtained from this clock source.
12 //! Timestamps are not comparable across operating system reboots.
13 //!
14 //! # Why not `std::time::Instant`?
15 //!
16 //! [`std::time::Instant`] fulfills some of our requirements:
17 //!
18 //! * It's monotonic, guaranteed ([sort of][rustsource]).
19 //! * It can be compared to other timespans.
20 //!
21 //! However:
22 //!
23 //! * It can't be serialized.
24 //! * It's not guaranteed that the clock source it uses contains suspend/hibernation time across all operating systems.
25 //!
26 //! [rustsource]: https://doc.rust-lang.org/1.47.0/src/std/time.rs.html#213-237
27 //!
28 //! # Example
29 //!
30 //! ```
31 //! # use std::{thread, time::Duration};
32 //! let start = zeitstempel::now();
33 //! thread::sleep(Duration::from_millis(2));
34 //!
35 //! let diff = Duration::from_nanos(zeitstempel::now() - start);
36 //! assert!(diff >= Duration::from_millis(2));
37 //! ```
38 //!
39 //! # Supported operating systems
40 //!
41 //! We support the following operating systems:
42 //!
43 //! * Windows\*
44 //! * macOS
45 //! * Linux
46 //! * Android
47 //! * iOS
48 //!
49 //! For other operating systems there's a fallback to `std::time::Instant`,
50 //! compared against a process-global fixed reference point.
51 //! We don't guarantee that measured time includes time the system spends in sleep or hibernation.
52 //!
53 //! \* To use native Windows 10 functionality enable the `win10plus` feature. Otherwise it will use the fallback.
54
55 #![deny(missing_docs)]
56 #![deny(broken_intra_doc_links)]
57
58 cfg_if::cfg_if! {
59 if #[cfg(any(target_os = "macos", target_os = "ios"))] {
60 mod mac;
61 use mac as sys;
62 } else if #[cfg(any(target_os = "linux", target_os = "android"))] {
63 mod linux;
64 use linux as sys;
65 } else if #[cfg(all(windows, feature = "win10plus"))] {
66 mod win;
67 use win as sys;
68 } else {
69 mod fallback;
70 use fallback as sys;
71 }
72 }
73
74 /// Returns a timestamp corresponding to "now".
75 ///
76 /// It can be compared to other timestamps gathered from this API, as long as the host was not
77 /// rebooted inbetween.
78 ///
79 ///
80 /// ## Note
81 ///
82 /// * The difference between two timestamps will include time the system was in sleep or
83 /// hibernation.
84 /// * The difference between two timestamps gathered from this is in nanoseconds.
85 /// * The clocks on some operating systems, e.g. on Windows, are not nanosecond-precise.
86 /// The value will still use nanosecond resolution.
87 pub fn now() -> u64 {
88 sys::now_including_suspend()
89 }
90
91 #[cfg(test)]
92 mod test {
93 use super::*;
94 use std::thread;
95 use std::time::Duration;
96
97 #[test]
98 fn order() {
99 let ts1 = now();
100 thread::sleep(Duration::from_millis(2));
101 let ts2 = now();
102
103 assert!(ts1 < ts2);
104 }
105 }