]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/redox/time.rs
New upstream version 1.33.0+dfsg1
[rustc.git] / src / libstd / sys / redox / time.rs
1 use cmp::Ordering;
2 use fmt;
3 use sys::{cvt, syscall};
4 use time::Duration;
5 use convert::TryInto;
6 use core::hash::{Hash, Hasher};
7
8 const NSEC_PER_SEC: u64 = 1_000_000_000;
9
10 #[derive(Copy, Clone)]
11 struct Timespec {
12 t: syscall::TimeSpec,
13 }
14
15 impl Timespec {
16 fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
17 if self >= other {
18 Ok(if self.t.tv_nsec >= other.t.tv_nsec {
19 Duration::new((self.t.tv_sec - other.t.tv_sec) as u64,
20 (self.t.tv_nsec - other.t.tv_nsec) as u32)
21 } else {
22 Duration::new((self.t.tv_sec - 1 - other.t.tv_sec) as u64,
23 self.t.tv_nsec as u32 + (NSEC_PER_SEC as u32) -
24 other.t.tv_nsec as u32)
25 })
26 } else {
27 match other.sub_timespec(self) {
28 Ok(d) => Err(d),
29 Err(d) => Ok(d),
30 }
31 }
32 }
33
34 fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
35 let mut secs = other
36 .as_secs()
37 .try_into() // <- target type would be `i64`
38 .ok()
39 .and_then(|secs| self.t.tv_sec.checked_add(secs))?;
40
41 // Nano calculations can't overflow because nanos are <1B which fit
42 // in a u32.
43 let mut nsec = other.subsec_nanos() + self.t.tv_nsec as u32;
44 if nsec >= NSEC_PER_SEC as u32 {
45 nsec -= NSEC_PER_SEC as u32;
46 secs = secs.checked_add(1)?;
47 }
48 Some(Timespec {
49 t: syscall::TimeSpec {
50 tv_sec: secs,
51 tv_nsec: nsec as i32,
52 },
53 })
54 }
55
56 fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
57 let mut secs = other
58 .as_secs()
59 .try_into() // <- target type would be `i64`
60 .ok()
61 .and_then(|secs| self.t.tv_sec.checked_sub(secs))?;
62
63 // Similar to above, nanos can't overflow.
64 let mut nsec = self.t.tv_nsec as i32 - other.subsec_nanos() as i32;
65 if nsec < 0 {
66 nsec += NSEC_PER_SEC as i32;
67 secs = secs.checked_sub(1)?;
68 }
69 Some(Timespec {
70 t: syscall::TimeSpec {
71 tv_sec: secs,
72 tv_nsec: nsec as i32,
73 },
74 })
75 }
76 }
77
78 impl PartialEq for Timespec {
79 fn eq(&self, other: &Timespec) -> bool {
80 self.t.tv_sec == other.t.tv_sec && self.t.tv_nsec == other.t.tv_nsec
81 }
82 }
83
84 impl Eq for Timespec {}
85
86 impl PartialOrd for Timespec {
87 fn partial_cmp(&self, other: &Timespec) -> Option<Ordering> {
88 Some(self.cmp(other))
89 }
90 }
91
92 impl Ord for Timespec {
93 fn cmp(&self, other: &Timespec) -> Ordering {
94 let me = (self.t.tv_sec, self.t.tv_nsec);
95 let other = (other.t.tv_sec, other.t.tv_nsec);
96 me.cmp(&other)
97 }
98 }
99
100 impl Hash for Timespec {
101 fn hash<H : Hasher>(&self, state: &mut H) {
102 self.t.tv_sec.hash(state);
103 self.t.tv_nsec.hash(state);
104 }
105 }
106
107 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
108 pub struct Instant {
109 t: Timespec,
110 }
111
112 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
113 pub struct SystemTime {
114 t: Timespec,
115 }
116
117 pub const UNIX_EPOCH: SystemTime = SystemTime {
118 t: Timespec {
119 t: syscall::TimeSpec {
120 tv_sec: 0,
121 tv_nsec: 0,
122 },
123 },
124 };
125
126 impl Instant {
127 pub fn now() -> Instant {
128 Instant { t: now(syscall::CLOCK_MONOTONIC) }
129 }
130
131 pub const fn zero() -> Instant {
132 Instant { t: Timespec { t: syscall::TimeSpec { tv_sec: 0, tv_nsec: 0 } } }
133 }
134
135 pub fn actually_monotonic() -> bool {
136 false
137 }
138
139 pub fn sub_instant(&self, other: &Instant) -> Duration {
140 self.t.sub_timespec(&other.t).unwrap_or_else(|_| {
141 panic!("specified instant was later than self")
142 })
143 }
144
145 pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
146 Some(Instant { t: self.t.checked_add_duration(other)? })
147 }
148
149 pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
150 Some(Instant { t: self.t.checked_sub_duration(other)? })
151 }
152 }
153
154 impl fmt::Debug for Instant {
155 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
156 f.debug_struct("Instant")
157 .field("tv_sec", &self.t.t.tv_sec)
158 .field("tv_nsec", &self.t.t.tv_nsec)
159 .finish()
160 }
161 }
162
163 impl SystemTime {
164 pub fn now() -> SystemTime {
165 SystemTime { t: now(syscall::CLOCK_REALTIME) }
166 }
167
168 pub fn sub_time(&self, other: &SystemTime)
169 -> Result<Duration, Duration> {
170 self.t.sub_timespec(&other.t)
171 }
172
173 pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
174 Some(SystemTime { t: self.t.checked_add_duration(other)? })
175 }
176
177 pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
178 Some(SystemTime { t: self.t.checked_sub_duration(other)? })
179 }
180 }
181
182 impl From<syscall::TimeSpec> for SystemTime {
183 fn from(t: syscall::TimeSpec) -> SystemTime {
184 SystemTime { t: Timespec { t } }
185 }
186 }
187
188 impl fmt::Debug for SystemTime {
189 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
190 f.debug_struct("SystemTime")
191 .field("tv_sec", &self.t.t.tv_sec)
192 .field("tv_nsec", &self.t.t.tv_nsec)
193 .finish()
194 }
195 }
196
197 pub type clock_t = usize;
198
199 fn now(clock: clock_t) -> Timespec {
200 let mut t = Timespec {
201 t: syscall::TimeSpec {
202 tv_sec: 0,
203 tv_nsec: 0,
204 }
205 };
206 cvt(syscall::clock_gettime(clock, &mut t.t)).unwrap();
207 t
208 }