]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/cloudabi/time.rs
Rollup merge of #61120 - spastorino:eval-place-iterate, r=oli-obk
[rust.git] / src / libstd / sys / cloudabi / time.rs
1 use crate::mem;
2 use crate::sys::cloudabi::abi;
3 use crate::time::Duration;
4
5 const NSEC_PER_SEC: abi::timestamp = 1_000_000_000;
6
7 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
8 pub struct Instant {
9     t: abi::timestamp,
10 }
11
12 pub fn checked_dur2intervals(dur: &Duration) -> Option<abi::timestamp> {
13     dur.as_secs()
14         .checked_mul(NSEC_PER_SEC)?
15         .checked_add(dur.subsec_nanos() as abi::timestamp)
16 }
17
18 impl Instant {
19     pub fn now() -> Instant {
20         unsafe {
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);
24             Instant { t }
25         }
26     }
27
28     pub fn actually_monotonic() -> bool {
29         true
30     }
31
32     pub const fn zero() -> Instant {
33         Instant { t: 0 }
34     }
35
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))
39     }
40
41     pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
42         Some(Instant {
43             t: self.t.checked_add(checked_dur2intervals(other)?)?,
44         })
45     }
46
47     pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
48         Some(Instant {
49             t: self.t.checked_sub(checked_dur2intervals(other)?)?,
50         })
51     }
52 }
53
54 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
55 pub struct SystemTime {
56     t: abi::timestamp,
57 }
58
59 impl SystemTime {
60     pub fn now() -> SystemTime {
61         unsafe {
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);
65             SystemTime { t }
66         }
67     }
68
69     pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
70         if self.t >= other.t {
71             let diff = self.t - other.t;
72             Ok(Duration::new(
73                 diff / NSEC_PER_SEC,
74                 (diff % NSEC_PER_SEC) as u32,
75             ))
76         } else {
77             let diff = other.t - self.t;
78             Err(Duration::new(
79                 diff / NSEC_PER_SEC,
80                 (diff % NSEC_PER_SEC) as u32,
81             ))
82         }
83     }
84
85     pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
86         Some(SystemTime {
87             t: self.t.checked_add(checked_dur2intervals(other)?)?,
88         })
89     }
90
91     pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
92         Some(SystemTime {
93             t: self.t.checked_sub(checked_dur2intervals(other)?)?,
94         })
95     }
96 }
97
98 pub const UNIX_EPOCH: SystemTime = SystemTime { t: 0 };