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
//! # clock-lib
//!
//! TIME READINGS FOR RUST
//!
//! Monotonic and wall-clock time readings with a mockable clock for deterministic
//! testing. Simple Tier-1 API, zero dependencies, no `unsafe`.
//!
//! # Two kinds of time
//!
//! There are two fundamentally different kinds of time, and conflating them is a
//! common source of bugs:
//!
//! - **Monotonic time** ([`now`]) never goes backwards. Use it to measure elapsed
//! time: rate limiting, timeouts, benchmarks. Only meaningful as a delta.
//! - **Wall-clock time** ([`wall`], [`unix`]) is calendar time. It can jump (NTP,
//! DST, manual changes). Use it for timestamps and logging, never for measuring
//! elapsed time.
//!
//! The two are returned as distinct types — [`Monotonic`] and [`Wall`] — so the
//! compiler rejects any attempt to subtract one from the other. That separation
//! is the central design choice of the crate.
//!
//! # Tier-1 API (the lazy path)
//!
//! ```
//! use clock_lib as clock;
//!
//! let start = clock::now(); // monotonic reading
//! // ... do work ...
//! let took = clock::elapsed(start); // Duration since `start`
//!
//! let secs = clock::unix(); // unix seconds (like PHP time())
//! # let _ = (took, secs);
//! ```
//!
//! # Tier-2 API (the mockable clock)
//!
//! The real value is deterministic time in tests. The [`Clock`] trait has two
//! implementations — [`SystemClock`] for production and [`ManualClock`]
//! for tests — so timing-driven code can be exercised without ever
//! calling `sleep`.
//!
//! ```
//! use clock_lib::{Clock, ManualClock, Monotonic};
//! use std::time::Duration;
//!
//! fn expired<C: Clock>(clock: &C, stamp: Monotonic, ttl: Duration) -> bool {
//! clock.now().duration_since(stamp) >= ttl
//! }
//!
//! let clock = ManualClock::new();
//! let stamp = clock.now();
//! assert!(!expired(&clock, stamp, Duration::from_secs(60)));
//!
//! clock.advance(Duration::from_secs(60));
//! assert!(expired(&clock, stamp, Duration::from_secs(60)));
//! ```
//!
//! # Feature flags
//!
//! - **`std`** (default): enables every reading API — [`Monotonic`],
//! [`Wall`], [`Clock`], [`SystemClock`], [`ManualClock`], and the Tier-1
//! free functions.
//!
//! Disable default features to build for a `no_std` target. With `std`
//! off, only [`VERSION`] is exposed — the readings themselves require
//! an operating system clock and cannot be portably provided without one.
//!
//! ```toml
//! [dependencies]
//! clock-lib = { version = "0.5", default-features = false }
//! ```
//!
//! # License
//!
//! Dual-licensed under Apache-2.0 OR MIT.
pub use ;
pub use Monotonic;
pub use Wall;
/// Crate version string, populated by Cargo at build time.
pub const VERSION: &str = env!;
/// Captures the current monotonic time.
///
/// Shortcut for [`Monotonic::now`]. Pair it with [`elapsed`] to measure how
/// long an operation took.
///
/// # Examples
///
/// ```
/// use clock_lib as clock;
///
/// let start = clock::now();
/// // ... work ...
/// let took = clock::elapsed(start);
/// # let _ = took;
/// ```
/// Returns the [`Duration`](core::time::Duration) elapsed since `earlier`.
///
/// Shortcut for [`Monotonic::elapsed`]. The argument is the
/// [`Monotonic`] captured at the start of the interval; the return value
/// is the time from then until now.
///
/// # Examples
///
/// ```
/// use clock_lib as clock;
///
/// let start = clock::now();
/// // ... work ...
/// let took = clock::elapsed(start);
/// # let _ = took;
/// ```
/// Captures the current wall-clock time.
///
/// Shortcut for [`Wall::now`]. Use it for timestamps. For elapsed-time
/// measurement, use [`now`] instead — wall-clock readings can jump.
///
/// # Examples
///
/// ```
/// use clock_lib as clock;
///
/// let stamp = clock::wall();
/// let secs = stamp.unix_seconds();
/// assert!(secs > 0);
/// ```
/// Returns the current Unix time in whole seconds.
///
/// Shortcut for `wall().unix_seconds()`. Equivalent in spirit to C's
/// `time(NULL)` or PHP's `time()`.
///
/// Returns zero if the system clock is set to a moment before the Unix
/// epoch.
///
/// # Examples
///
/// ```
/// use clock_lib as clock;
///
/// let now = clock::unix();
/// assert!(now > 0);
/// ```
/// Returns the current Unix time in whole milliseconds.
///
/// Shortcut for `wall().unix_millis()`. Returns zero if the system clock is
/// set to a moment before the Unix epoch.
///
/// # Examples
///
/// ```
/// use clock_lib as clock;
///
/// let now = clock::unix_ms();
/// assert!(now > 0);
/// ```
/// Returns the current Unix time in whole nanoseconds.
///
/// Shortcut for `wall().unix_nanos()`. Returns zero if the system clock is
/// set to a moment before the Unix epoch.
///
/// # Examples
///
/// ```
/// use clock_lib as clock;
///
/// let now = clock::unix_ns();
/// assert!(now > 0);
/// ```