]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/redox/time.rs
dea406efe6ca963d9788c01114fd81c053de0c21
[rust.git] / src / libstd / sys / redox / time.rs
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.
4 //
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.
10
11 use cmp::Ordering;
12 use fmt;
13 use sys::{cvt, syscall};
14 use time::Duration;
15
16 const NSEC_PER_SEC: u64 = 1_000_000_000;
17
18 #[derive(Copy, Clone)]
19 struct Timespec {
20     t: syscall::TimeSpec,
21 }
22
23 impl Timespec {
24     fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
25         if self >= other {
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)
29             } else {
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)
33             })
34         } else {
35             match other.sub_timespec(self) {
36                 Ok(d) => Err(d),
37                 Err(d) => Ok(d),
38             }
39         }
40     }
41
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");
45
46         // Nano calculations can't overflow because nanos are <1B which fit
47         // in a u32.
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 \
52                                                duration to time");
53         }
54         Timespec {
55             t: syscall::TimeSpec {
56                 tv_sec: secs as i64,
57                 tv_nsec: nsec as i32,
58             },
59         }
60     }
61
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 \
65                                     from time");
66
67         // Similar to above, nanos can't overflow.
68         let mut nsec = self.t.tv_nsec as i32 - other.subsec_nanos() as i32;
69         if nsec < 0 {
70             nsec += NSEC_PER_SEC as i32;
71             secs = secs.checked_sub(1).expect("overflow when subtracting \
72                                                duration from time");
73         }
74         Timespec {
75             t: syscall::TimeSpec {
76                 tv_sec: secs as i64,
77                 tv_nsec: nsec as i32,
78             },
79         }
80     }
81 }
82
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
86     }
87 }
88
89 impl Eq for Timespec {}
90
91 impl PartialOrd for Timespec {
92     fn partial_cmp(&self, other: &Timespec) -> Option<Ordering> {
93         Some(self.cmp(other))
94     }
95 }
96
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);
101         me.cmp(&other)
102     }
103 }
104
105 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
106 pub struct Instant {
107     t: Timespec,
108 }
109
110 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
111 pub struct SystemTime {
112     t: Timespec,
113 }
114
115 pub const UNIX_EPOCH: SystemTime = SystemTime {
116     t: Timespec {
117         t: syscall::TimeSpec {
118             tv_sec: 0,
119             tv_nsec: 0,
120         },
121     },
122 };
123
124 impl Instant {
125     pub fn now() -> Instant {
126         Instant { t: now(syscall::CLOCK_MONOTONIC) }
127     }
128
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")
132         })
133     }
134
135     pub fn add_duration(&self, other: &Duration) -> Instant {
136         Instant { t: self.t.add_duration(other) }
137     }
138
139     pub fn sub_duration(&self, other: &Duration) -> Instant {
140         Instant { t: self.t.sub_duration(other) }
141     }
142 }
143
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)
149          .finish()
150     }
151 }
152
153 impl SystemTime {
154     pub fn now() -> SystemTime {
155         SystemTime { t: now(syscall::CLOCK_REALTIME) }
156     }
157
158     pub fn sub_time(&self, other: &SystemTime)
159                     -> Result<Duration, Duration> {
160         self.t.sub_timespec(&other.t)
161     }
162
163     pub fn add_duration(&self, other: &Duration) -> SystemTime {
164         SystemTime { t: self.t.add_duration(other) }
165     }
166
167     pub fn sub_duration(&self, other: &Duration) -> SystemTime {
168         SystemTime { t: self.t.sub_duration(other) }
169     }
170 }
171
172 impl From<syscall::TimeSpec> for SystemTime {
173     fn from(t: syscall::TimeSpec) -> SystemTime {
174         SystemTime { t: Timespec { t: t } }
175     }
176 }
177
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)
183          .finish()
184     }
185 }
186
187 pub type clock_t = usize;
188
189 fn now(clock: clock_t) -> Timespec {
190     let mut t = Timespec {
191         t: syscall::TimeSpec {
192             tv_sec: 0,
193             tv_nsec: 0,
194         }
195     };
196     cvt(syscall::clock_gettime(clock, &mut t.t)).unwrap();
197     t
198 }