use friendly::temporal::HumanDuration;
use std::fmt;
use std::time::{Duration, Instant};
#[derive(Debug, Clone)]
pub struct Timer {
started: Instant,
}
impl Timer {
pub fn new() -> Timer {
Timer {
started: Instant::now(),
}
}
pub fn elapsed(&self) -> Duration {
self.started.elapsed()
}
pub fn human_elapsed(&self) -> HumanDuration {
self.elapsed().into()
}
}
impl fmt::Display for Timer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.human_elapsed())
}
}
#[cfg(test)]
pub fn human_time(dur: Duration) -> String {
let hd = HumanDuration::from(dur);
hd.to_string()
}
#[test]
fn test_human_secs() {
let s = human_time(Duration::from_secs(10));
assert_eq!(s.as_str(), "10.00s");
}
#[test]
fn test_human_mins() {
let s = human_time(Duration::from_secs(135));
assert_eq!(s.as_str(), "2m15.00s");
}