1 // Copyright 2016 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.
13 use sys::{cvt, syscall};
16 const NSEC_PER_SEC: u64 = 1_000_000_000;
18 #[derive(Copy, Clone)]
24 fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
26 Ok(if self.t.tv_nsec >= other.t.tv_nsec {
27 Duration::new((self.t.tv_sec - other.t.tv_sec) as u64,
28 (self.t.tv_nsec - other.t.tv_nsec) as u32)
30 Duration::new((self.t.tv_sec - 1 - other.t.tv_sec) as u64,
31 self.t.tv_nsec as u32 + (NSEC_PER_SEC as u32) -
32 other.t.tv_nsec as u32)
35 match other.sub_timespec(self) {
42 fn add_duration(&self, other: &Duration) -> Timespec {
43 let secs = (self.t.tv_sec as i64).checked_add(other.as_secs() as i64);
44 let mut secs = secs.expect("overflow when adding duration to time");
46 // Nano calculations can't overflow because nanos are <1B which fit
48 let mut nsec = other.subsec_nanos() + self.t.tv_nsec as u32;
49 if nsec >= NSEC_PER_SEC as u32 {
50 nsec -= NSEC_PER_SEC as u32;
51 secs = secs.checked_add(1).expect("overflow when adding \
55 t: syscall::TimeSpec {
62 fn sub_duration(&self, other: &Duration) -> Timespec {
63 let secs = (self.t.tv_sec as i64).checked_sub(other.as_secs() as i64);
64 let mut secs = secs.expect("overflow when subtracting duration \
67 // Similar to above, nanos can't overflow.
68 let mut nsec = self.t.tv_nsec as i32 - other.subsec_nanos() as i32;
70 nsec += NSEC_PER_SEC as i32;
71 secs = secs.checked_sub(1).expect("overflow when subtracting \
75 t: syscall::TimeSpec {
83 impl PartialEq for Timespec {
84 fn eq(&self, other: &Timespec) -> bool {
85 self.t.tv_sec == other.t.tv_sec && self.t.tv_nsec == other.t.tv_nsec
89 impl Eq for Timespec {}
91 impl PartialOrd for Timespec {
92 fn partial_cmp(&self, other: &Timespec) -> Option<Ordering> {
97 impl Ord for Timespec {
98 fn cmp(&self, other: &Timespec) -> Ordering {
99 let me = (self.t.tv_sec, self.t.tv_nsec);
100 let other = (other.t.tv_sec, other.t.tv_nsec);
105 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
110 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
111 pub struct SystemTime {
115 pub const UNIX_EPOCH: SystemTime = SystemTime {
117 t: syscall::TimeSpec {
125 pub fn now() -> Instant {
126 Instant { t: now(syscall::CLOCK_MONOTONIC) }
129 pub fn sub_instant(&self, other: &Instant) -> Duration {
130 self.t.sub_timespec(&other.t).unwrap_or_else(|_| {
131 panic!("other was less than the current instant")
135 pub fn add_duration(&self, other: &Duration) -> Instant {
136 Instant { t: self.t.add_duration(other) }
139 pub fn sub_duration(&self, other: &Duration) -> Instant {
140 Instant { t: self.t.sub_duration(other) }
144 impl fmt::Debug for Instant {
145 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
146 f.debug_struct("Instant")
147 .field("tv_sec", &self.t.t.tv_sec)
148 .field("tv_nsec", &self.t.t.tv_nsec)
154 pub fn now() -> SystemTime {
155 SystemTime { t: now(syscall::CLOCK_REALTIME) }
158 pub fn sub_time(&self, other: &SystemTime)
159 -> Result<Duration, Duration> {
160 self.t.sub_timespec(&other.t)
163 pub fn add_duration(&self, other: &Duration) -> SystemTime {
164 SystemTime { t: self.t.add_duration(other) }
167 pub fn sub_duration(&self, other: &Duration) -> SystemTime {
168 SystemTime { t: self.t.sub_duration(other) }
172 impl From<syscall::TimeSpec> for SystemTime {
173 fn from(t: syscall::TimeSpec) -> SystemTime {
174 SystemTime { t: Timespec { t: t } }
178 impl fmt::Debug for SystemTime {
179 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
180 f.debug_struct("SystemTime")
181 .field("tv_sec", &self.t.t.tv_sec)
182 .field("tv_nsec", &self.t.t.tv_nsec)
187 pub type clock_t = usize;
189 fn now(clock: clock_t) -> Timespec {
190 let mut t = Timespec {
191 t: syscall::TimeSpec {
196 cvt(syscall::clock_gettime(clock, &mut t.t)).unwrap();