1 use crate::cmp::Ordering;
3 use crate::time::Duration;
4 use ::core::hash::{Hash, Hasher};
6 pub use self::inner::{Instant, SystemTime, UNIX_EPOCH};
7 use crate::convert::TryInto;
9 const NSEC_PER_SEC: u64 = 1_000_000_000;
11 #[derive(Copy, Clone)]
17 const fn zero() -> Timespec {
19 t: libc::timespec { tv_sec: 0, tv_nsec: 0 },
22 fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
24 Ok(if self.t.tv_nsec >= other.t.tv_nsec {
25 Duration::new((self.t.tv_sec - other.t.tv_sec) as u64,
26 (self.t.tv_nsec - other.t.tv_nsec) as u32)
28 Duration::new((self.t.tv_sec - 1 - other.t.tv_sec) as u64,
29 self.t.tv_nsec as u32 + (NSEC_PER_SEC as u32) -
30 other.t.tv_nsec as u32)
33 match other.sub_timespec(self) {
40 fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
43 .try_into() // <- target type would be `libc::time_t`
45 .and_then(|secs| self.t.tv_sec.checked_add(secs))?;
47 // Nano calculations can't overflow because nanos are <1B which fit
49 let mut nsec = other.subsec_nanos() + self.t.tv_nsec as u32;
50 if nsec >= NSEC_PER_SEC as u32 {
51 nsec -= NSEC_PER_SEC as u32;
52 secs = secs.checked_add(1)?;
62 fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
65 .try_into() // <- target type would be `libc::time_t`
67 .and_then(|secs| self.t.tv_sec.checked_sub(secs))?;
69 // Similar to above, nanos can't overflow.
70 let mut nsec = self.t.tv_nsec as i32 - other.subsec_nanos() as i32;
72 nsec += NSEC_PER_SEC as i32;
73 secs = secs.checked_sub(1)?;
84 impl PartialEq for Timespec {
85 fn eq(&self, other: &Timespec) -> bool {
86 self.t.tv_sec == other.t.tv_sec && self.t.tv_nsec == other.t.tv_nsec
90 impl Eq for Timespec {}
92 impl PartialOrd for Timespec {
93 fn partial_cmp(&self, other: &Timespec) -> Option<Ordering> {
98 impl Ord for Timespec {
99 fn cmp(&self, other: &Timespec) -> Ordering {
100 let me = (self.t.tv_sec, self.t.tv_nsec);
101 let other = (other.t.tv_sec, other.t.tv_nsec);
106 impl Hash for Timespec {
107 fn hash<H : Hasher>(&self, state: &mut H) {
108 self.t.tv_sec.hash(state);
109 self.t.tv_nsec.hash(state);
116 use crate::time::Duration;
120 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
125 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
126 pub struct SystemTime {
130 pub const UNIX_EPOCH: SystemTime = SystemTime {
140 pub fn now() -> Instant {
141 Instant { t: now(libc::CLOCK_MONOTONIC) }
144 pub const fn zero() -> Instant {
150 pub fn actually_monotonic() -> bool {
154 pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
155 self.t.sub_timespec(&other.t).ok()
158 pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
159 Some(Instant { t: self.t.checked_add_duration(other)? })
162 pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
163 Some(Instant { t: self.t.checked_sub_duration(other)? })
167 impl fmt::Debug for Instant {
168 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169 f.debug_struct("Instant")
170 .field("tv_sec", &self.t.t.tv_sec)
171 .field("tv_nsec", &self.t.t.tv_nsec)
177 pub fn now() -> SystemTime {
178 SystemTime { t: now(libc::CLOCK_REALTIME) }
181 pub fn sub_time(&self, other: &SystemTime)
182 -> Result<Duration, Duration> {
183 self.t.sub_timespec(&other.t)
186 pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
187 Some(SystemTime { t: self.t.checked_add_duration(other)? })
190 pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
191 Some(SystemTime { t: self.t.checked_sub_duration(other)? })
195 impl From<libc::timespec> for SystemTime {
196 fn from(t: libc::timespec) -> SystemTime {
197 SystemTime { t: Timespec { t: t } }
201 impl fmt::Debug for SystemTime {
202 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
203 f.debug_struct("SystemTime")
204 .field("tv_sec", &self.t.t.tv_sec)
205 .field("tv_nsec", &self.t.t.tv_nsec)
210 pub type clock_t = libc::c_int;
212 fn now(clock: clock_t) -> Timespec {
213 let mut t = Timespec {
220 libc::clock_gettime(clock, &mut t.t)