2 use crate::sys::cloudabi::abi;
3 use crate::time::Duration;
5 const NSEC_PER_SEC: abi::timestamp = 1_000_000_000;
7 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
12 pub fn checked_dur2intervals(dur: &Duration) -> Option<abi::timestamp> {
14 .checked_mul(NSEC_PER_SEC)?
15 .checked_add(dur.subsec_nanos() as abi::timestamp)
19 pub fn now() -> Instant {
21 let mut t = mem::uninitialized();
22 let ret = abi::clock_time_get(abi::clockid::MONOTONIC, 0, &mut t);
23 assert_eq!(ret, abi::errno::SUCCESS);
28 pub fn actually_monotonic() -> bool {
32 pub const fn zero() -> Instant {
36 pub fn sub_instant(&self, other: &Instant) -> Duration {
39 .expect("second instant is later than self");
40 Duration::new(diff / NSEC_PER_SEC, (diff % NSEC_PER_SEC) as u32)
43 pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
45 t: self.t.checked_add(checked_dur2intervals(other)?)?,
49 pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
51 t: self.t.checked_sub(checked_dur2intervals(other)?)?,
56 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
57 pub struct SystemTime {
62 pub fn now() -> SystemTime {
64 let mut t = mem::uninitialized();
65 let ret = abi::clock_time_get(abi::clockid::REALTIME, 0, &mut t);
66 assert_eq!(ret, abi::errno::SUCCESS);
71 pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
72 if self.t >= other.t {
73 let diff = self.t - other.t;
76 (diff % NSEC_PER_SEC) as u32,
79 let diff = other.t - self.t;
82 (diff % NSEC_PER_SEC) as u32,
87 pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
89 t: self.t.checked_add(checked_dur2intervals(other)?)?,
93 pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
95 t: self.t.checked_sub(checked_dur2intervals(other)?)?,
100 pub const UNIX_EPOCH: SystemTime = SystemTime { t: 0 };