]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/hermit/time.rs
Rollup merge of #82686 - CDirkx:unix-platform, r=m-ou-se
[rust.git] / library / std / src / sys / hermit / time.rs
1 #![allow(dead_code)]
2
3 use crate::cmp::Ordering;
4 use crate::convert::TryInto;
5 use crate::sys::hermit::abi;
6 use crate::sys::hermit::abi::timespec;
7 use crate::sys::hermit::abi::{CLOCK_MONOTONIC, CLOCK_REALTIME, NSEC_PER_SEC};
8 use crate::time::Duration;
9 use core::hash::{Hash, Hasher};
10
11 #[derive(Copy, Clone, Debug)]
12 struct Timespec {
13     t: timespec,
14 }
15
16 impl Timespec {
17     const fn zero() -> Timespec {
18         Timespec { t: timespec { tv_sec: 0, tv_nsec: 0 } }
19     }
20
21     fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
22         if self >= other {
23             Ok(if self.t.tv_nsec >= other.t.tv_nsec {
24                 Duration::new(
25                     (self.t.tv_sec - other.t.tv_sec) as u64,
26                     (self.t.tv_nsec - other.t.tv_nsec) as u32,
27                 )
28             } else {
29                 Duration::new(
30                     (self.t.tv_sec - 1 - other.t.tv_sec) as u64,
31                     self.t.tv_nsec as u32 + (NSEC_PER_SEC as u32) - other.t.tv_nsec as u32,
32                 )
33             })
34         } else {
35             match other.sub_timespec(self) {
36                 Ok(d) => Err(d),
37                 Err(d) => Ok(d),
38             }
39         }
40     }
41
42     fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
43         let mut secs = other
44             .as_secs()
45             .try_into() // <- target type would be `libc::time_t`
46             .ok()
47             .and_then(|secs| self.t.tv_sec.checked_add(secs))?;
48
49         // Nano calculations can't overflow because nanos are <1B which fit
50         // in a u32.
51         let mut nsec = other.subsec_nanos() + self.t.tv_nsec as u32;
52         if nsec >= NSEC_PER_SEC as u32 {
53             nsec -= NSEC_PER_SEC as u32;
54             secs = secs.checked_add(1)?;
55         }
56         Some(Timespec { t: timespec { tv_sec: secs, tv_nsec: nsec as _ } })
57     }
58
59     fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
60         let mut secs = other
61             .as_secs()
62             .try_into() // <- target type would be `libc::time_t`
63             .ok()
64             .and_then(|secs| self.t.tv_sec.checked_sub(secs))?;
65
66         // Similar to above, nanos can't overflow.
67         let mut nsec = self.t.tv_nsec as i32 - other.subsec_nanos() as i32;
68         if nsec < 0 {
69             nsec += NSEC_PER_SEC as i32;
70             secs = secs.checked_sub(1)?;
71         }
72         Some(Timespec { t: timespec { tv_sec: secs, tv_nsec: nsec as _ } })
73     }
74 }
75
76 impl PartialEq for Timespec {
77     fn eq(&self, other: &Timespec) -> bool {
78         self.t.tv_sec == other.t.tv_sec && self.t.tv_nsec == other.t.tv_nsec
79     }
80 }
81
82 impl Eq for Timespec {}
83
84 impl PartialOrd for Timespec {
85     fn partial_cmp(&self, other: &Timespec) -> Option<Ordering> {
86         Some(self.cmp(other))
87     }
88 }
89
90 impl Ord for Timespec {
91     fn cmp(&self, other: &Timespec) -> Ordering {
92         let me = (self.t.tv_sec, self.t.tv_nsec);
93         let other = (other.t.tv_sec, other.t.tv_nsec);
94         me.cmp(&other)
95     }
96 }
97
98 impl Hash for Timespec {
99     fn hash<H: Hasher>(&self, state: &mut H) {
100         self.t.tv_sec.hash(state);
101         self.t.tv_nsec.hash(state);
102     }
103 }
104
105 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
106 pub struct Instant {
107     t: Timespec,
108 }
109
110 impl Instant {
111     pub fn now() -> Instant {
112         let mut time: Timespec = Timespec::zero();
113         let _ = unsafe { abi::clock_gettime(CLOCK_MONOTONIC, &mut time.t as *mut timespec) };
114
115         Instant { t: time }
116     }
117
118     pub const fn zero() -> Instant {
119         Instant { t: Timespec::zero() }
120     }
121
122     pub fn actually_monotonic() -> bool {
123         true
124     }
125
126     pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
127         self.t.sub_timespec(&other.t).ok()
128     }
129
130     pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
131         Some(Instant { t: self.t.checked_add_duration(other)? })
132     }
133
134     pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
135         Some(Instant { t: self.t.checked_sub_duration(other)? })
136     }
137 }
138
139 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
140 pub struct SystemTime {
141     t: Timespec,
142 }
143
144 pub const UNIX_EPOCH: SystemTime = SystemTime { t: Timespec::zero() };
145
146 impl SystemTime {
147     pub fn now() -> SystemTime {
148         let mut time: Timespec = Timespec::zero();
149         let _ = unsafe { abi::clock_gettime(CLOCK_REALTIME, &mut time.t as *mut timespec) };
150
151         SystemTime { t: time }
152     }
153
154     pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
155         self.t.sub_timespec(&other.t)
156     }
157
158     pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
159         Some(SystemTime { t: self.t.checked_add_duration(other)? })
160     }
161
162     pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
163         Some(SystemTime { t: self.t.checked_sub_duration(other)? })
164     }
165 }