1
1
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} ;
10
3
11
4
const MICROS_PER_SEC : i64 = 1_000_000 ;
12
5
const SECS_PER_MINUTE : i64 = 60 ;
13
6
const SECS_PER_HOUR : i64 = 3600 ;
14
7
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
+ }
17
12
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
+ }
20
18
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
+ }
22
24
23
25
impl TimeVal {
24
26
#[ inline]
@@ -44,8 +46,8 @@ impl TimeVal {
44
46
45
47
#[ inline]
46
48
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 } }
49
51
}
50
52
51
53
#[ inline]
@@ -60,8 +62,8 @@ impl TimeVal {
60
62
#[ inline]
61
63
pub fn microseconds ( microseconds : i64 ) -> TimeVal {
62
64
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 } }
65
67
}
66
68
67
69
pub fn num_hours ( & self ) -> i64 {
@@ -73,10 +75,10 @@ impl TimeVal {
73
75
}
74
76
75
77
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
78
80
} else {
79
- self . tv_sec as i64
81
+ self . val . tv_sec as i64
80
82
}
81
83
}
82
84
@@ -91,10 +93,10 @@ impl TimeVal {
91
93
}
92
94
93
95
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
96
98
} else {
97
- self . tv_usec
99
+ self . val . tv_usec
98
100
}
99
101
}
100
102
}
@@ -147,32 +149,40 @@ impl ops::Div<i32> for TimeVal {
147
149
148
150
impl fmt:: Display for TimeVal {
149
151
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 {
151
153
( -* self , "-" )
152
154
} else {
153
155
( * self , "" )
154
156
} ;
155
157
156
- let sec = abs. tv_sec ;
158
+ let sec = abs. val . tv_sec ;
157
159
158
160
try!( write ! ( f, "{}" , sign) ) ;
159
161
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 {
162
164
try!( write ! ( f, "{} second" , sec) ) ;
163
165
} else {
164
166
try!( write ! ( f, "{} seconds" , sec) ) ;
165
167
}
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 ) ) ;
168
170
} else {
169
- try!( write ! ( f, "{}.{:06} seconds" , sec, abs. tv_usec) ) ;
171
+ try!( write ! ( f, "{}.{:06} seconds" , sec, abs. val . tv_usec) ) ;
170
172
}
171
173
172
174
Ok ( ( ) )
173
175
}
174
176
}
175
177
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
+
176
186
#[ inline]
177
187
fn div_mod_floor_64 ( this : i64 , other : i64 ) -> ( i64 , i64 ) {
178
188
( div_floor_64 ( this, other) , mod_floor_64 ( this, other) )
@@ -208,17 +218,16 @@ mod test {
208
218
#[ test]
209
219
pub fn test_time_val ( ) {
210
220
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 ) ) ;
214
223
}
215
224
216
225
#[ test]
217
226
pub fn test_time_val_neg ( ) {
218
227
let a = TimeVal :: seconds ( 1 ) + TimeVal :: microseconds ( 123 ) ;
219
228
let b = TimeVal :: seconds ( -1 ) + TimeVal :: microseconds ( -123 ) ;
220
229
221
- assert_eq ! ( a, -b) ;
230
+ assert ! ( a == -b) ;
222
231
}
223
232
224
233
#[ test]
0 commit comments