1use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
27
28#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
36pub struct Timestamp {
37 nanos: i64,
38}
39
40fn origin() -> Instant {
42 use std::sync::OnceLock;
43 static ORIGIN: OnceLock<Instant> = OnceLock::new();
44 *ORIGIN.get_or_init(Instant::now)
45}
46
47impl Timestamp {
48 pub fn now() -> Self {
50 let elapsed = origin().elapsed();
51 Self {
52 nanos: elapsed.as_nanos().min(i64::MAX as u128) as i64,
53 }
54 }
55
56 pub const fn zero() -> Self {
58 Self { nanos: 0 }
59 }
60
61 pub const fn new_from_nanos(n: i64) -> Self {
63 Self { nanos: n }
64 }
65
66 pub fn new_from_secs_f64(s: f64) -> Self {
68 Self {
69 nanos: (s * 1e9) as i64,
70 }
71 }
72
73 pub const fn as_nanos(self) -> i64 {
75 self.nanos
76 }
77
78 pub fn to_secs(self) -> f64 {
80 self.nanos as f64 / 1e9
81 }
82
83 pub fn to_ms(self) -> f64 {
85 self.nanos as f64 / 1e6
86 }
87
88 pub fn is_valid(self) -> bool {
91 self.nanos > 0
92 }
93}
94
95impl std::ops::Sub for Timestamp {
96 type Output = Timestamp;
97 fn sub(self, rhs: Self) -> Self {
98 Self {
99 nanos: self.nanos.saturating_sub(rhs.nanos),
100 }
101 }
102}
103
104impl std::ops::Add for Timestamp {
105 type Output = Timestamp;
106 fn add(self, rhs: Self) -> Self {
107 Self {
108 nanos: self.nanos.saturating_add(rhs.nanos),
109 }
110 }
111}
112
113impl std::ops::SubAssign for Timestamp {
114 fn sub_assign(&mut self, rhs: Self) {
115 self.nanos = self.nanos.saturating_sub(rhs.nanos);
116 }
117}
118
119impl std::ops::AddAssign for Timestamp {
120 fn add_assign(&mut self, rhs: Self) {
121 self.nanos = self.nanos.saturating_add(rhs.nanos);
122 }
123}
124
125pub struct Stopwatch {
137 start: Timestamp,
138 elapsed: Timestamp,
139 running: bool,
140}
141
142impl Default for Stopwatch {
143 fn default() -> Self {
144 Self::new()
145 }
146}
147
148impl Stopwatch {
149 pub fn new() -> Self {
151 Self {
152 start: Timestamp::zero(),
153 elapsed: Timestamp::zero(),
154 running: false,
155 }
156 }
157
158 pub fn start(&mut self) {
160 if !self.running {
161 self.start = Timestamp::now();
162 self.running = true;
163 }
164 }
165
166 pub fn stop(&mut self) {
168 if self.running {
169 self.elapsed += Timestamp::now() - self.start;
170 self.running = false;
171 }
172 }
173
174 pub fn reset(&mut self) {
176 self.elapsed = Timestamp::zero();
177 self.running = false;
178 }
179
180 pub fn restart(&mut self) {
182 self.reset();
183 self.start();
184 }
185
186 pub fn elapsed(&self) -> Timestamp {
188 if self.running {
189 self.elapsed + (Timestamp::now() - self.start)
190 } else {
191 self.elapsed
192 }
193 }
194
195 pub fn elapsed_sec(&self) -> f64 {
197 self.elapsed().to_secs()
198 }
199
200 pub fn elapsed_ms(&self) -> f64 {
202 self.elapsed().to_ms()
203 }
204
205 pub fn is_running(&self) -> bool {
207 self.running
208 }
209}
210
211pub struct ScopedTimer<'a> {
226 output: &'a mut f64,
227 start: Timestamp,
228}
229
230impl<'a> ScopedTimer<'a> {
231 pub fn new(output: &'a mut f64) -> Self {
233 Self {
234 output,
235 start: Timestamp::now(),
236 }
237 }
238}
239
240impl Drop for ScopedTimer<'_> {
241 fn drop(&mut self) {
242 *self.output = (Timestamp::now() - self.start).to_secs();
243 }
244}
245
246#[derive(Clone, Copy, Debug, PartialEq, Eq)]
253pub struct Datetime {
254 unix_secs: i64,
255}
256
257impl Datetime {
258 pub fn now() -> Self {
260 let secs = SystemTime::now()
261 .duration_since(UNIX_EPOCH)
262 .unwrap_or(Duration::ZERO)
263 .as_secs();
264 Self {
265 unix_secs: secs as i64,
266 }
267 }
268
269 pub const fn new_from_unix_secs(secs: i64) -> Self {
271 Self { unix_secs: secs }
272 }
273
274 pub const fn as_unix_secs(self) -> i64 {
276 self.unix_secs
277 }
278}
279
280impl std::fmt::Display for Datetime {
281 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
283 let s = self.unix_secs.unsigned_abs();
284 let secs = s % 60;
285 let mins = (s / 60) % 60;
286 let hours = (s / 3600) % 24;
287 let days = s / 86400;
288 let (y, m, d) = civil_from_days(days);
290 write!(f, "{y:04}-{m:02}-{d:02} {hours:02}:{mins:02}:{secs:02}")
291 }
292}
293
294fn civil_from_days(z: u64) -> (u32, u32, u32) {
297 let z = z as i64 + 719_468;
298 let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
299 let doe = (z - era * 146_097) as u32;
300 let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
301 let y = yoe as i64 + era * 400;
302 let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
303 let mp = (5 * doy + 2) / 153;
304 let d = doy - (153 * mp + 2) / 5 + 1;
305 let m = if mp < 10 { mp + 3 } else { mp - 9 };
306 let y = if m <= 2 { y + 1 } else { y };
307 (y as u32, m, d)
308}