#[cfg(not(windows))]
use rustix::time::{clock_getres, ClockId};
use std::time::{self, Duration};
pub trait SystemClockExt {
type SystemTime;
fn now_with(&self, precision: Duration) -> Self::SystemTime;
fn resolution(&self) -> Duration;
}
#[cfg(not(windows))]
impl SystemClockExt for cap_primitives::time::SystemClock {
type SystemTime = cap_primitives::time::SystemTime;
#[cfg(not(target_os = "wasi"))]
#[inline]
fn now_with(&self, _precision: Duration) -> Self::SystemTime {
Self::SystemTime::from_std(time::SystemTime::now())
}
fn resolution(&self) -> Duration {
let spec = clock_getres(ClockId::Realtime);
Duration::new(
spec.tv_sec.try_into().unwrap(),
spec.tv_nsec.try_into().unwrap(),
)
}
}
#[cfg(windows)]
impl SystemClockExt for cap_primitives::time::SystemClock {
type SystemTime = cap_primitives::time::SystemTime;
#[inline]
fn now_with(&self, _precision: Duration) -> Self::SystemTime {
Self::SystemTime::from_std(time::SystemTime::now())
}
fn resolution(&self) -> Duration {
Duration::new(0, 55_000_000)
}
}