1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
15 pub use self::inner::{Instant, SystemTime, UNIX_EPOCH};
17 const NSEC_PER_SEC: u64 = 1_000_000_000;
19 #[derive(Copy, Clone)]
25 fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
27 Ok(if self.t.tv_nsec >= other.t.tv_nsec {
28 Duration::new((self.t.tv_sec - other.t.tv_sec) as u64,
29 (self.t.tv_nsec - other.t.tv_nsec) as u32)
31 Duration::new((self.t.tv_sec - 1 - other.t.tv_sec) as u64,
32 self.t.tv_nsec as u32 + (NSEC_PER_SEC as u32) -
33 other.t.tv_nsec as u32)
36 match other.sub_timespec(self) {
43 fn add_duration(&self, other: &Duration) -> Timespec {
44 let secs = (self.t.tv_sec as i64).checked_add(other.as_secs() as i64);
45 let mut secs = secs.expect("overflow when adding duration to time");
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).expect("overflow when adding \
57 tv_sec: secs as libc::time_t,
58 tv_nsec: nsec as libc::c_long,
63 fn sub_duration(&self, other: &Duration) -> Timespec {
64 let secs = (self.t.tv_sec as i64).checked_sub(other.as_secs() as i64);
65 let mut secs = secs.expect("overflow when subtracting duration \
68 // Similar to above, nanos can't overflow.
69 let mut nsec = self.t.tv_nsec as i32 - other.subsec_nanos() as i32;
71 nsec += NSEC_PER_SEC as i32;
72 secs = secs.checked_sub(1).expect("overflow when subtracting \
77 tv_sec: secs as libc::time_t,
78 tv_nsec: nsec as libc::c_long,
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 #[cfg(any(target_os = "macos", target_os = "ios"))]
112 use sys_common::mul_div_u64;
115 use super::NSEC_PER_SEC;
118 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
123 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
124 pub struct SystemTime {
128 pub const UNIX_EPOCH: SystemTime = SystemTime {
138 pub fn now() -> Instant {
139 Instant { t: unsafe { libc::mach_absolute_time() } }
142 pub fn sub_instant(&self, other: &Instant) -> Duration {
144 let diff = self.t.checked_sub(other.t)
145 .expect("second instant is later than self");
146 let nanos = mul_div_u64(diff, info.numer as u64, info.denom as u64);
147 Duration::new(nanos / NSEC_PER_SEC, (nanos % NSEC_PER_SEC) as u32)
150 pub fn add_duration(&self, other: &Duration) -> Instant {
152 t: self.t.checked_add(dur2intervals(other))
153 .expect("overflow when adding duration to instant"),
157 pub fn sub_duration(&self, other: &Duration) -> Instant {
159 t: self.t.checked_sub(dur2intervals(other))
160 .expect("overflow when adding duration to instant"),
166 pub fn now() -> SystemTime {
169 let mut s = libc::timeval {
174 libc::gettimeofday(&mut s, ptr::null_mut())
176 return SystemTime::from(s)
179 pub fn sub_time(&self, other: &SystemTime)
180 -> Result<Duration, Duration> {
181 self.t.sub_timespec(&other.t)
184 pub fn add_duration(&self, other: &Duration) -> SystemTime {
185 SystemTime { t: self.t.add_duration(other) }
188 pub fn sub_duration(&self, other: &Duration) -> SystemTime {
189 SystemTime { t: self.t.sub_duration(other) }
193 impl From<libc::timeval> for SystemTime {
194 fn from(t: libc::timeval) -> SystemTime {
195 SystemTime::from(libc::timespec {
197 tv_nsec: (t.tv_usec * 1000) as libc::c_long,
202 impl From<libc::timespec> for SystemTime {
203 fn from(t: libc::timespec) -> SystemTime {
204 SystemTime { t: Timespec { t: t } }
208 impl fmt::Debug for SystemTime {
209 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
210 f.debug_struct("SystemTime")
211 .field("tv_sec", &self.t.t.tv_sec)
212 .field("tv_nsec", &self.t.t.tv_nsec)
217 fn dur2intervals(dur: &Duration) -> u64 {
219 let nanos = dur.as_secs().checked_mul(NSEC_PER_SEC).and_then(|nanos| {
220 nanos.checked_add(dur.subsec_nanos() as u64)
221 }).expect("overflow converting duration to nanoseconds");
222 mul_div_u64(nanos, info.denom as u64, info.numer as u64)
225 fn info() -> &'static libc::mach_timebase_info {
226 static mut INFO: libc::mach_timebase_info = libc::mach_timebase_info {
230 static ONCE: Once = Once::new();
234 libc::mach_timebase_info(&mut INFO);
241 #[cfg(not(any(target_os = "macos", target_os = "ios")))]
250 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
255 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
256 pub struct SystemTime {
260 pub const UNIX_EPOCH: SystemTime = SystemTime {
270 pub fn now() -> Instant {
271 Instant { t: now(libc::CLOCK_MONOTONIC) }
274 pub fn sub_instant(&self, other: &Instant) -> Duration {
275 self.t.sub_timespec(&other.t).unwrap_or_else(|_| {
276 panic!("other was less than the current instant")
280 pub fn add_duration(&self, other: &Duration) -> Instant {
281 Instant { t: self.t.add_duration(other) }
284 pub fn sub_duration(&self, other: &Duration) -> Instant {
285 Instant { t: self.t.sub_duration(other) }
289 impl fmt::Debug for Instant {
290 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
291 f.debug_struct("Instant")
292 .field("tv_sec", &self.t.t.tv_sec)
293 .field("tv_nsec", &self.t.t.tv_nsec)
299 pub fn now() -> SystemTime {
300 SystemTime { t: now(libc::CLOCK_REALTIME) }
303 pub fn sub_time(&self, other: &SystemTime)
304 -> Result<Duration, Duration> {
305 self.t.sub_timespec(&other.t)
308 pub fn add_duration(&self, other: &Duration) -> SystemTime {
309 SystemTime { t: self.t.add_duration(other) }
312 pub fn sub_duration(&self, other: &Duration) -> SystemTime {
313 SystemTime { t: self.t.sub_duration(other) }
317 impl From<libc::timespec> for SystemTime {
318 fn from(t: libc::timespec) -> SystemTime {
319 SystemTime { t: Timespec { t: t } }
323 impl fmt::Debug for SystemTime {
324 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
325 f.debug_struct("SystemTime")
326 .field("tv_sec", &self.t.t.tv_sec)
327 .field("tv_nsec", &self.t.t.tv_nsec)
332 #[cfg(not(target_os = "dragonfly"))]
333 pub type clock_t = libc::c_int;
334 #[cfg(target_os = "dragonfly")]
335 pub type clock_t = libc::c_ulong;
337 fn now(clock: clock_t) -> Timespec {
338 let mut t = Timespec {
345 libc::clock_gettime(clock, &mut t.t)