Skip to content

Commit c4a38ec

Browse files
committed
time: use libc::timeval for TimeVal
1 parent dbce477 commit c4a38ec

File tree

1 file changed

+43
-34
lines changed

1 file changed

+43
-34
lines changed

src/sys/time.rs

+43-34
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
use std::{fmt, ops};
2-
use libc::{time_t, suseconds_t};
3-
4-
#[repr(C)]
5-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
6-
pub struct TimeVal {
7-
pub tv_sec: time_t,
8-
pub tv_usec: suseconds_t,
9-
}
2+
use libc::{time_t, suseconds_t, timeval};
103

114
const MICROS_PER_SEC: i64 = 1_000_000;
125
const SECS_PER_MINUTE: i64 = 60;
136
const SECS_PER_HOUR: i64 = 3600;
147

15-
#[cfg(target_pointer_width = "64")]
16-
const MAX_SECONDS: i64 = (::std::i64::MAX / MICROS_PER_SEC) - 1;
8+
#[derive(Clone, Copy)]
9+
pub struct TimeVal {
10+
pub val: timeval,
11+
}
1712

18-
#[cfg(target_pointer_width = "32")]
19-
const MAX_SECONDS: i64 = ::std::isize::MAX as i64;
13+
impl AsRef<timeval> for TimeVal {
14+
fn as_ref(&self) -> &timeval {
15+
&self.val
16+
}
17+
}
2018

21-
const MIN_SECONDS: i64 = -MAX_SECONDS;
19+
impl AsMut<timeval> for TimeVal {
20+
fn as_mut(&mut self) -> &mut timeval {
21+
&mut self.val
22+
}
23+
}
2224

2325
impl TimeVal {
2426
#[inline]
@@ -44,8 +46,8 @@ impl TimeVal {
4446

4547
#[inline]
4648
pub fn seconds(seconds: i64) -> TimeVal {
47-
assert!(seconds >= MIN_SECONDS && seconds <= MAX_SECONDS, "TimeVal out of bounds; seconds={}", seconds);
48-
TimeVal { tv_sec: seconds as time_t, tv_usec: 0 }
49+
assert!(seconds >= time_t::min_value() && seconds <= time_t::max_value(), "TimeVal out of bounds; seconds={}", seconds);
50+
TimeVal { val: timeval { tv_sec: seconds as time_t, tv_usec: 0 } }
4951
}
5052

5153
#[inline]
@@ -60,8 +62,8 @@ impl TimeVal {
6062
#[inline]
6163
pub fn microseconds(microseconds: i64) -> TimeVal {
6264
let (secs, micros) = div_mod_floor_64(microseconds, MICROS_PER_SEC);
63-
assert!(secs >= MIN_SECONDS && secs <= MAX_SECONDS, "TimeVal out of bounds");
64-
TimeVal { tv_sec: secs as time_t, tv_usec: micros as suseconds_t }
65+
assert!(secs >= time_t::min_value() && secs <= time_t::max_value(), "TimeVal out of bounds; seconds={}", secs);
66+
TimeVal { val: timeval { tv_sec: secs as time_t, tv_usec: micros as suseconds_t } }
6567
}
6668

6769
pub fn num_hours(&self) -> i64 {
@@ -73,10 +75,10 @@ impl TimeVal {
7375
}
7476

7577
pub fn num_seconds(&self) -> i64 {
76-
if self.tv_sec < 0 && self.tv_usec > 0 {
77-
(self.tv_sec + 1) as i64
78+
if self.val.tv_sec < 0 && self.val.tv_usec > 0 {
79+
(self.val.tv_sec + 1) as i64
7880
} else {
79-
self.tv_sec as i64
81+
self.val.tv_sec as i64
8082
}
8183
}
8284

@@ -91,10 +93,10 @@ impl TimeVal {
9193
}
9294

9395
fn micros_mod_sec(&self) -> suseconds_t {
94-
if self.tv_sec < 0 && self.tv_usec > 0 {
95-
self.tv_usec - MICROS_PER_SEC as suseconds_t
96+
if self.val.tv_sec < 0 && self.val.tv_usec > 0 {
97+
self.val.tv_usec - MICROS_PER_SEC as suseconds_t
9698
} else {
97-
self.tv_usec
99+
self.val.tv_usec
98100
}
99101
}
100102
}
@@ -147,32 +149,40 @@ impl ops::Div<i32> for TimeVal {
147149

148150
impl fmt::Display for TimeVal {
149151
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
150-
let (abs, sign) = if self.tv_sec < 0 {
152+
let (abs, sign) = if self.val.tv_sec < 0 {
151153
(-*self, "-")
152154
} else {
153155
(*self, "")
154156
};
155157

156-
let sec = abs.tv_sec;
158+
let sec = abs.val.tv_sec;
157159

158160
try!(write!(f, "{}", sign));
159161

160-
if abs.tv_usec == 0 {
161-
if abs.tv_sec == 1 {
162+
if abs.val.tv_usec == 0 {
163+
if abs.val.tv_sec == 1 {
162164
try!(write!(f, "{} second", sec));
163165
} else {
164166
try!(write!(f, "{} seconds", sec));
165167
}
166-
} else if abs.tv_usec % 1000 == 0 {
167-
try!(write!(f, "{}.{:03} seconds", sec, abs.tv_usec / 1000));
168+
} else if abs.val.tv_usec % 1_000 == 0 {
169+
try!(write!(f, "{}.{:03} seconds", sec, abs.val.tv_usec / 1_000));
168170
} else {
169-
try!(write!(f, "{}.{:06} seconds", sec, abs.tv_usec));
171+
try!(write!(f, "{}.{:06} seconds", sec, abs.val.tv_usec));
170172
}
171173

172174
Ok(())
173175
}
174176
}
175177

178+
impl PartialEq for TimeVal {
179+
fn eq(&self, rhs: &TimeVal) -> bool {
180+
self.val.tv_sec == rhs.val.tv_sec && self.val.tv_usec == rhs.val.tv_usec
181+
}
182+
}
183+
184+
impl Eq for TimeVal { }
185+
176186
#[inline]
177187
fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) {
178188
(div_floor_64(this, other), mod_floor_64(this, other))
@@ -208,17 +218,16 @@ mod test {
208218
#[test]
209219
pub fn test_time_val() {
210220
assert!(TimeVal::seconds(1) != TimeVal::zero());
211-
assert_eq!(TimeVal::seconds(1) + TimeVal::seconds(2), TimeVal::seconds(3));
212-
assert_eq!(TimeVal::minutes(3) + TimeVal::seconds(2),
213-
TimeVal::seconds(182));
221+
assert!(TimeVal::seconds(1) + TimeVal::seconds(2) == TimeVal::seconds(3));
222+
assert!(TimeVal::minutes(3) + TimeVal::seconds(2) == TimeVal::seconds(182));
214223
}
215224

216225
#[test]
217226
pub fn test_time_val_neg() {
218227
let a = TimeVal::seconds(1) + TimeVal::microseconds(123);
219228
let b = TimeVal::seconds(-1) + TimeVal::microseconds(-123);
220229

221-
assert_eq!(a, -b);
230+
assert!(a == -b);
222231
}
223232

224233
#[test]

0 commit comments

Comments
 (0)