|
| 1 | +use crate::time::Duration; |
| 2 | + |
| 3 | +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] |
| 4 | +pub struct Instant(Duration); |
| 5 | + |
| 6 | +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] |
| 7 | +pub struct SystemTime(Duration); |
| 8 | + |
| 9 | +pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::from_secs(0)); |
| 10 | + |
| 11 | +impl Instant { |
| 12 | + pub fn now() -> Instant { |
| 13 | + let result = crate::os::xous::ffi::blocking_scalar( |
| 14 | + crate::os::xous::services::ticktimer_server(), |
| 15 | + [0 /* ElapsedMs */, 0, 0, 0, 0], |
| 16 | + ) |
| 17 | + .expect("failed to request elapsed_ms"); |
| 18 | + let lower = result[0]; |
| 19 | + let upper = result[1]; |
| 20 | + Instant { 0: Duration::from_millis(lower as u64 | (upper as u64) << 32) } |
| 21 | + } |
| 22 | + |
| 23 | + pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { |
| 24 | + self.0.checked_sub(other.0) |
| 25 | + } |
| 26 | + |
| 27 | + pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { |
| 28 | + Some(Instant(self.0.checked_add(*other)?)) |
| 29 | + } |
| 30 | + |
| 31 | + pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> { |
| 32 | + Some(Instant(self.0.checked_sub(*other)?)) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl SystemTime { |
| 37 | + pub fn now() -> SystemTime { |
| 38 | + let result = crate::os::xous::ffi::blocking_scalar( |
| 39 | + crate::os::xous::services::ticktimer_server(), |
| 40 | + [3 /* GetUtcTimeMs */, 0, 0, 0, 0], |
| 41 | + ) |
| 42 | + .expect("failed to request utc time in ms"); |
| 43 | + let lower = result[0]; |
| 44 | + let upper = result[1]; |
| 45 | + SystemTime { 0: Duration::from_millis((upper as u64) << 32 | lower as u64) } |
| 46 | + } |
| 47 | + |
| 48 | + pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> { |
| 49 | + self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0) |
| 50 | + } |
| 51 | + |
| 52 | + pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> { |
| 53 | + Some(SystemTime(self.0.checked_add(*other)?)) |
| 54 | + } |
| 55 | + |
| 56 | + pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> { |
| 57 | + Some(SystemTime(self.0.checked_sub(*other)?)) |
| 58 | + } |
| 59 | +} |
0 commit comments