use std::time::{Duration, SystemTime, Instant, UNIX_EPOCH};
#[cfg(test)]
use tokio_timer::clock;
#[cfg(test)]
use tokio_timer::clock::Now;
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 {
clock::now()
}
#[cfg(not(test))]
pub fn clock_now() -> Instant {
Instant::now()
}
pub fn clock_elapsed(time: Instant) -> Duration {
clock_now() - time
}
#[cfg(test)]
pub struct ConstNow(pub Instant);
#[cfg(test)]
impl Now for ConstNow {
fn now(&self) -> Instant {
self.0
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use tokio_executor;
use tokio_timer::clock::*;
#[test]
fn const_elapsed() {
let now = clock_now();
let duration = Duration::from_secs(42);
let clock = Clock::new_with_now(ConstNow(now + duration));
let mut enter = tokio_executor::enter().unwrap();
with_default(&clock, &mut enter, |_| {
let elapsed = clock_elapsed(now);
assert_eq!(elapsed, duration);
});
}
}