]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/cloudabi/time.rs
Improve some compiletest documentation
[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 sub_instant(&self, other: &Instant) -> Duration {
37         let diff = self.t
38             .checked_sub(other.t)
39             .expect("second instant is later than self");
40         Duration::new(diff / NSEC_PER_SEC, (diff % NSEC_PER_SEC) as u32)
41     }
42
43     pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
44         Some(Instant {
45             t: self.t.checked_add(checked_dur2intervals(other)?)?,
46         })
47     }
48
49     pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
50         Some(Instant {
51             t: self.t.checked_sub(checked_dur2intervals(other)?)?,
52         })
53     }
54 }
55
56 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
57 pub struct SystemTime {
58     t: abi::timestamp,
59 }
60
61 impl SystemTime {
62     pub fn now() -> SystemTime {
63         unsafe {
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);
67             SystemTime { t }
68         }
69     }
70
71     pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
72         if self.t >= other.t {
73             let diff = self.t - other.t;
74             Ok(Duration::new(
75                 diff / NSEC_PER_SEC,
76                 (diff % NSEC_PER_SEC) as u32,
77             ))
78         } else {
79             let diff = other.t - self.t;
80             Err(Duration::new(
81                 diff / NSEC_PER_SEC,
82                 (diff % NSEC_PER_SEC) as u32,
83             ))
84         }
85     }
86
87     pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
88         Some(SystemTime {
89             t: self.t.checked_add(checked_dur2intervals(other)?)?,
90         })
91     }
92
93     pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
94         Some(SystemTime {
95             t: self.t.checked_sub(checked_dur2intervals(other)?)?,
96         })
97     }
98 }
99
100 pub const UNIX_EPOCH: SystemTime = SystemTime { t: 0 };