3 use crate::cmp::Ordering;
4 use crate::sys::hermit::abi;
5 use crate::sys::hermit::abi::timespec;
6 use crate::sys::hermit::abi::{CLOCK_MONOTONIC, CLOCK_REALTIME, NSEC_PER_SEC};
7 use crate::time::Duration;
8 use core::hash::{Hash, Hasher};
10 #[derive(Copy, Clone, Debug)]
16 const fn zero() -> Timespec {
17 Timespec { t: timespec { tv_sec: 0, tv_nsec: 0 } }
20 fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
22 Ok(if self.t.tv_nsec >= other.t.tv_nsec {
24 (self.t.tv_sec - other.t.tv_sec) as u64,
25 (self.t.tv_nsec - other.t.tv_nsec) as u32,
29 (self.t.tv_sec - 1 - other.t.tv_sec) as u64,
30 self.t.tv_nsec as u32 + (NSEC_PER_SEC as u32) - other.t.tv_nsec as u32,
34 match other.sub_timespec(self) {
41 fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
44 .try_into() // <- target type would be `libc::time_t`
46 .and_then(|secs| self.t.tv_sec.checked_add(secs))?;
48 // Nano calculations can't overflow because nanos are <1B which fit
50 let mut nsec = other.subsec_nanos() + self.t.tv_nsec as u32;
51 if nsec >= NSEC_PER_SEC as u32 {
52 nsec -= NSEC_PER_SEC as u32;
53 secs = secs.checked_add(1)?;
55 Some(Timespec { t: timespec { tv_sec: secs, tv_nsec: nsec as _ } })
58 fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
61 .try_into() // <- target type would be `libc::time_t`
63 .and_then(|secs| self.t.tv_sec.checked_sub(secs))?;
65 // Similar to above, nanos can't overflow.
66 let mut nsec = self.t.tv_nsec as i32 - other.subsec_nanos() as i32;
68 nsec += NSEC_PER_SEC as i32;
69 secs = secs.checked_sub(1)?;
71 Some(Timespec { t: timespec { tv_sec: secs, tv_nsec: nsec as _ } })
75 impl PartialEq for Timespec {
76 fn eq(&self, other: &Timespec) -> bool {
77 self.t.tv_sec == other.t.tv_sec && self.t.tv_nsec == other.t.tv_nsec
81 impl Eq for Timespec {}
83 impl PartialOrd for Timespec {
84 fn partial_cmp(&self, other: &Timespec) -> Option<Ordering> {
89 impl Ord for Timespec {
90 fn cmp(&self, other: &Timespec) -> Ordering {
91 let me = (self.t.tv_sec, self.t.tv_nsec);
92 let other = (other.t.tv_sec, other.t.tv_nsec);
97 impl Hash for Timespec {
98 fn hash<H: Hasher>(&self, state: &mut H) {
99 self.t.tv_sec.hash(state);
100 self.t.tv_nsec.hash(state);
104 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
110 pub fn now() -> Instant {
111 let mut time: Timespec = Timespec::zero();
112 let _ = unsafe { abi::clock_gettime(CLOCK_MONOTONIC, &mut time.t as *mut timespec) };
117 pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
118 self.t.sub_timespec(&other.t).ok()
121 pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
122 Some(Instant { t: self.t.checked_add_duration(other)? })
125 pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
126 Some(Instant { t: self.t.checked_sub_duration(other)? })
130 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
131 pub struct SystemTime {
135 pub const UNIX_EPOCH: SystemTime = SystemTime { t: Timespec::zero() };
138 pub fn now() -> SystemTime {
139 let mut time: Timespec = Timespec::zero();
140 let _ = unsafe { abi::clock_gettime(CLOCK_REALTIME, &mut time.t as *mut timespec) };
142 SystemTime { t: time }
145 pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
146 self.t.sub_timespec(&other.t)
149 pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
150 Some(SystemTime { t: self.t.checked_add_duration(other)? })
153 pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
154 Some(SystemTime { t: self.t.checked_sub_duration(other)? })