instant_web.rs 756 B
use std::ops::Sub;
use std::time::Duration;
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
pub struct Spot {
/// Millisecond offset from the Unix Epoch - equivalent to Date.now()
inner: f64,
}
impl Spot {
pub fn now() -> Self {
Spot {
inner: js_sys::Date::now(),
}
}
pub fn as_secs(&self) -> u64 {
self.as_duration().as_secs()
}
pub fn as_duration(&self) -> Duration {
Self::now() - *self
}
pub fn elapsed(&self) -> Duration {
self.as_duration()
}
}
impl Sub<Spot> for Spot {
type Output = Duration;
fn sub(self, rhs: Spot) -> Self::Output {
let diff = (self.inner - rhs.inner).max(0.0);
let secs = (diff as u64) / 1_000;
let nanos = (((diff as u64) % 1_000) as u32) * 1_000_000;
Duration::new(secs, nanos)
}
}