use std::time::{Duration, SystemTime, Instant, UNIX_EPOCH};
pub fn unix_time(time: SystemTime) -> u64 {
let since_the_epoch = time.duration_since(UNIX_EPOCH)
.expect("Current time is earlier than Unix epoch");
since_the_epoch.as_secs()
}
#[cfg(test)]
pub fn clock_now() -> Instant {
tokio::time::Instant::now().into_std()
}
#[cfg(not(test))]
pub fn clock_now() -> Instant {
Instant::now()
}
pub fn clock_elapsed(time: Instant) -> Duration {
clock_now() - time
}
#[cfg(test)]
pub mod tests {
use super::{clock_now, clock_elapsed};
#[tokio::test]
async fn const_elapsed() {
tokio::time::pause();
let now = clock_now();
let duration = std::time::Duration::from_secs(42);
tokio::time::advance(duration).await;
let elapsed = clock_elapsed(now);
assert_eq!(elapsed, duration);
}
}