cap_time_ext/
system_clock.rs1#[cfg(not(windows))]
2use rustix::time::{clock_getres, ClockId};
3use std::time::{self, Duration};
4
5pub trait SystemClockExt {
7 type SystemTime;
9
10 fn now_with(&self, precision: Duration) -> Self::SystemTime;
15
16 fn resolution(&self) -> Duration;
18}
19
20#[cfg(not(windows))]
21impl SystemClockExt for cap_primitives::time::SystemClock {
22 type SystemTime = cap_primitives::time::SystemTime;
23
24 #[cfg(not(target_os = "wasi"))]
25 #[inline]
26 fn now_with(&self, _precision: Duration) -> Self::SystemTime {
27 Self::SystemTime::from_std(time::SystemTime::now())
30 }
31
32 fn resolution(&self) -> Duration {
33 let spec = clock_getres(ClockId::Realtime);
34 Duration::new(
35 spec.tv_sec.try_into().unwrap(),
36 spec.tv_nsec.try_into().unwrap(),
37 )
38 }
39}
40
41#[cfg(windows)]
42impl SystemClockExt for cap_primitives::time::SystemClock {
43 type SystemTime = cap_primitives::time::SystemTime;
44
45 #[inline]
46 fn now_with(&self, _precision: Duration) -> Self::SystemTime {
47 Self::SystemTime::from_std(time::SystemTime::now())
50 }
51
52 fn resolution(&self) -> Duration {
53 Duration::new(0, 55_000_000)
58 }
59}