1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//! Suspend-time is a cross-platform monotonic clock that is suspend-unaware, written in Rust!
//! It allows system suspension (e.g. when a user closes their laptop on windows) to not affect
//! `Instant` durations and timeouts!
//!
//! Example of using [`SuspendUnawareInstant`]:
//! ```
//! use std::{thread, time};
//! use suspend_time::{SuspendUnawareInstant};
//!
//! fn main() {
//! // If you used std::time::Instant here and you suspend the system on windows,
//! // it will print more than 3 seconds (circa July 2024).
//! // With SuspendUnawareInstant this has no effect.
//! let instant = SuspendUnawareInstant::now();
//! let three_secs = time::Duration::from_secs(3);
//! thread::sleep(three_secs);
//! println!("{:#?}", instant.elapsed());
//! }
//! ```
//!
//! Example of using `suspend_time::`[`timeout`]
//!
//! ```
//! use std::time::Duration;
//!
//! #[tokio::main]
//! async fn main() {
//! // If you suspend the system during main's execution, Tokio will time
//! // out even though it only slept for 1 second. suspend_time::timeout does not.
//! let _ = suspend_time::timeout(
//! Duration::from_secs(2),
//! suspend_time::sleep(Duration::from_secs(1)),
//! ).await;
//! }
//! ```
//!
use ;
const NANOS_PER_SECOND: u32 = 1_000_000_000;
/// Similar to the standard library's implementation of
/// [`Instant`](https://doc.rust-lang.org/1.78.0/std/time/struct.Instant.html),
/// except it is consistently unaware of system suspends across all platforms
/// supported by this library.
///
/// Historically, this has been inconsistent in the standard library, with
/// windows allowing time to pass when the system is suspended/hibernating,
/// however unix systems do not "pass time" during system suspension. In this
/// library, time **never passes** when the system is suspended on **any
/// platform**.
///
/// This instant implementation is:
/// - Opaque (you cannot manually create an Instant. You must call ::now())
/// - Cross platform (windows, macOS)
/// - Monotonic (time never goes backwards)
/// - Suspend-unaware (when you put your computer to sleep, "time" does not pass.)
///
/// # Undefined behavior / Invariants
/// 1. When polling the system clock, nanoseconds should never exceed 10^9 (the number of nanoseconds in 1 second).
/// If this happens, we simply return zero. The standard library has a similar invariant (0 <= nanos <= 10^9), but handles it differently.
/// 2. If an instant in the future is subtracted from an instant in the past, we return a Duration of 0.
/// 3. If a duration is subtracted that would cause an instant to be negative, we return an instant set at 0.
/// 4. If a duration is added to an instant that would cause the instant to exceed 2^64 seconds, we return an instant set to 0.
///
/// # Underlying System calls
///
/// The following system calls are currently being used by `now()` to find out
/// the current time:
///
/// | Platform | System call |
/// |-----------|---------------------------------------------------------|
/// | UNIX | [clock_gettime] (CLOCK_UPTIME_RAW) |
/// | Darwin | [clock_gettime] (CLOCK_UPTIME_RAW) |
/// | VXWorks | [clock_gettime] (CLOCK_UPTIME_RAW) |
/// | Windows | [QueryUnbiasedInterruptTimePrecise] |
///
/// [clock_gettime]: https://www.manpagez.com/man/3/clock_gettime/
/// [QueryUnbiasedInterruptTimePrecise]:
/// https://learn.microsoft.com/en-us/windows/win32/api/realtimeapiset/nf-realtimeapiset-queryunbiasedinterrupttimeprecise
///
/// Certain overflows are dependent on how the standard library implements
/// Duration. For example, right now it is implemented as a u64 counting
/// seconds. As such, to prevent overflow we must check if the number of seconds
/// in two Durations exceeds the bounds of a u64. To avoid being dependent on
/// the standard library for cases like this, we choose our own representation
/// of time which matches the "apple" libc platform implementation.
// When adding/subtracting a `Duration` to/from a SuspendUnawareInstant, we want
// the result to be a new instant (point in time)
/// Suspend-time's equivalent of tokio's `tokio::time::error::Elapsed`.
/// Constructing the `Elapsed` struct is impossible due to its private construct
/// and private members. As such, we must create our own struct
;
/// The same API as tokio::time::timeout, except it is uses on SuspendUnawareInstant for measuring time.
pub async
/// The same API as tokio::time::sleep, except it is uses on SuspendUnawareInstant for measuring time.
pub async