1 use crate::cmp::Ordering;
2 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 },
23 fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
25 Ok(if self.t.tv_nsec >= other.t.tv_nsec {
26 Duration::new((self.t.tv_sec - other.t.tv_sec) as u64,
27 (self.t.tv_nsec - other.t.tv_nsec) as u32)
29 Duration::new((self.t.tv_sec - 1 - other.t.tv_sec) as u64,
30 self.t.tv_nsec as u32 + (NSEC_PER_SEC as u32) -
31 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)?;
63 fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
66 .try_into() // <- target type would be `libc::time_t`
68 .and_then(|secs| self.t.tv_sec.checked_sub(secs))?;
70 // Similar to above, nanos can't overflow.
71 let mut nsec = self.t.tv_nsec as i32 - other.subsec_nanos() as i32;
73 nsec += NSEC_PER_SEC as i32;
74 secs = secs.checked_sub(1)?;
85 impl PartialEq for Timespec {
86 fn eq(&self, other: &Timespec) -> bool {
87 self.t.tv_sec == other.t.tv_sec && self.t.tv_nsec == other.t.tv_nsec
91 impl Eq for Timespec {}
93 impl PartialOrd for Timespec {
94 fn partial_cmp(&self, other: &Timespec) -> Option<Ordering> {
99 impl Ord for Timespec {
100 fn cmp(&self, other: &Timespec) -> Ordering {
101 let me = (self.t.tv_sec, self.t.tv_nsec);
102 let other = (other.t.tv_sec, other.t.tv_nsec);
107 impl Hash for Timespec {
108 fn hash<H : Hasher>(&self, state: &mut H) {
109 self.t.tv_sec.hash(state);
110 self.t.tv_nsec.hash(state);
114 #[cfg(any(target_os = "macos", target_os = "ios"))]
117 use crate::sync::Once;
119 use crate::sys_common::mul_div_u64;
120 use crate::time::Duration;
122 use super::NSEC_PER_SEC;
125 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
130 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
131 pub struct SystemTime {
135 pub const UNIX_EPOCH: SystemTime = SystemTime {
140 pub fn now() -> Instant {
141 Instant { t: unsafe { libc::mach_absolute_time() } }
144 pub const fn zero() -> Instant {
148 pub fn actually_monotonic() -> bool {
152 pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
153 let diff = self.t.checked_sub(other.t)?;
155 let nanos = mul_div_u64(diff, info.numer as u64, info.denom as u64);
156 Some(Duration::new(nanos / NSEC_PER_SEC, (nanos % NSEC_PER_SEC) as u32))
159 pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
161 t: self.t.checked_add(checked_dur2intervals(other)?)?,
165 pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
167 t: self.t.checked_sub(checked_dur2intervals(other)?)?,
173 pub fn now() -> SystemTime {
176 let mut s = libc::timeval {
181 libc::gettimeofday(&mut s, ptr::null_mut())
183 return SystemTime::from(s)
186 pub fn sub_time(&self, other: &SystemTime)
187 -> Result<Duration, Duration> {
188 self.t.sub_timespec(&other.t)
191 pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
192 Some(SystemTime { t: self.t.checked_add_duration(other)? })
195 pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
196 Some(SystemTime { t: self.t.checked_sub_duration(other)? })
200 impl From<libc::timeval> for SystemTime {
201 fn from(t: libc::timeval) -> SystemTime {
202 SystemTime::from(libc::timespec {
204 tv_nsec: (t.tv_usec * 1000) as libc::c_long,
209 impl From<libc::timespec> for SystemTime {
210 fn from(t: libc::timespec) -> SystemTime {
211 SystemTime { t: Timespec { t } }
215 impl fmt::Debug for SystemTime {
216 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
217 f.debug_struct("SystemTime")
218 .field("tv_sec", &self.t.t.tv_sec)
219 .field("tv_nsec", &self.t.t.tv_nsec)
224 fn checked_dur2intervals(dur: &Duration) -> Option<u64> {
225 let nanos = dur.as_secs()
226 .checked_mul(NSEC_PER_SEC)?
227 .checked_add(dur.subsec_nanos() as u64)?;
229 Some(mul_div_u64(nanos, info.denom as u64, info.numer as u64))
232 fn info() -> &'static libc::mach_timebase_info {
233 static mut INFO: libc::mach_timebase_info = libc::mach_timebase_info {
237 static ONCE: Once = Once::new();
241 libc::mach_timebase_info(&mut INFO);
248 #[cfg(not(any(target_os = "macos", target_os = "ios")))]
252 use crate::time::Duration;
256 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
261 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
262 pub struct SystemTime {
266 pub const UNIX_EPOCH: SystemTime = SystemTime {
271 pub fn now() -> Instant {
272 Instant { t: now(libc::CLOCK_MONOTONIC) }
275 pub const fn zero() -> Instant {
281 pub fn actually_monotonic() -> bool {
282 (cfg!(target_os = "linux") && cfg!(target_arch = "x86_64")) ||
283 (cfg!(target_os = "linux") && cfg!(target_arch = "x86")) ||
284 false // last clause, used so `||` is always trailing above
287 pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
288 self.t.sub_timespec(&other.t).ok()
291 pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
292 Some(Instant { t: self.t.checked_add_duration(other)? })
295 pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
296 Some(Instant { t: self.t.checked_sub_duration(other)? })
300 impl fmt::Debug for Instant {
301 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
302 f.debug_struct("Instant")
303 .field("tv_sec", &self.t.t.tv_sec)
304 .field("tv_nsec", &self.t.t.tv_nsec)
310 pub fn now() -> SystemTime {
311 SystemTime { t: now(libc::CLOCK_REALTIME) }
314 pub fn sub_time(&self, other: &SystemTime)
315 -> Result<Duration, Duration> {
316 self.t.sub_timespec(&other.t)
319 pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
320 Some(SystemTime { t: self.t.checked_add_duration(other)? })
323 pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
324 Some(SystemTime { t: self.t.checked_sub_duration(other)? })
328 impl From<libc::timespec> for SystemTime {
329 fn from(t: libc::timespec) -> SystemTime {
330 SystemTime { t: Timespec { t } }
334 impl fmt::Debug for SystemTime {
335 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
336 f.debug_struct("SystemTime")
337 .field("tv_sec", &self.t.t.tv_sec)
338 .field("tv_nsec", &self.t.t.tv_nsec)
343 #[cfg(not(any(target_os = "dragonfly", target_os = "hermit")))]
344 pub type clock_t = libc::c_int;
345 #[cfg(any(target_os = "dragonfly", target_os = "hermit"))]
346 pub type clock_t = libc::c_ulong;
348 fn now(clock: clock_t) -> Timespec {
349 let mut t = Timespec {
356 libc::clock_gettime(clock, &mut t.t)