pub trait DurationSinceEpoch {
fn as_secs(&self) -> u64;
fn as_millis(&self) -> u64;
}
#[cfg(feature = "std")]
use std::convert::From;
#[cfg(feature = "std")]
use std::time::{Duration, SystemTime};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg(feature = "std")]
pub struct StdDurationSinceEpoch(Duration);
#[cfg(feature = "std")]
impl StdDurationSinceEpoch {
pub fn now() -> Self {
StdDurationSinceEpoch(
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("SystemTime should be later than UNIX_EPOCH."),
)
}
}
#[cfg(feature = "std")]
impl DurationSinceEpoch for StdDurationSinceEpoch {
fn as_secs(&self) -> u64 {
self.0.as_secs()
}
fn as_millis(&self) -> u64 {
self.0.as_secs() * 1000 + u64::from(self.0.subsec_millis())
}
}