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 checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
37 let diff = self.t.checked_sub(other.t)?;
38 Some(Duration::new(diff / NSEC_PER_SEC, (diff % NSEC_PER_SEC) as u32))
41 pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
43 t: self.t.checked_add(checked_dur2intervals(other)?)?,
47 pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
49 t: self.t.checked_sub(checked_dur2intervals(other)?)?,
54 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
55 pub struct SystemTime {
60 pub fn now() -> SystemTime {
62 let mut t = mem::uninitialized();
63 let ret = abi::clock_time_get(abi::clockid::REALTIME, 0, &mut t);
64 assert_eq!(ret, abi::errno::SUCCESS);
69 pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
70 if self.t >= other.t {
71 let diff = self.t - other.t;
74 (diff % NSEC_PER_SEC) as u32,
77 let diff = other.t - self.t;
80 (diff % NSEC_PER_SEC) as u32,
85 pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
87 t: self.t.checked_add(checked_dur2intervals(other)?)?,
91 pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
93 t: self.t.checked_sub(checked_dur2intervals(other)?)?,
98 pub const UNIX_EPOCH: SystemTime = SystemTime { t: 0 };