use core::time::Duration;
use crate::{fs::File, read_value};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Instant {
value: u64,
}
impl Instant {
pub fn now() -> Self {
let mut timer = File::open("/dev/timer0").expect("failed to open timer file");
let current: u64 = read_value(&mut timer).expect("failed to read current time");
Self {
value: current
}
}
pub fn duration_since(&self, earlier: Instant) -> Duration {
let diff = self.value.saturating_sub(earlier.value);
Duration::from_nanos(diff)
}
pub fn elapsed(&self) -> Duration {
self.duration_since(Instant::now())
}
}