10
10
11
11
#[ forbid( deprecated_mode) ] ;
12
12
13
- use core:: cmp:: Eq ;
13
+ use core:: cmp:: { Eq , Ord } ;
14
14
use core:: int;
15
15
use core:: libc:: { c_char, c_int, c_long, size_t, time_t} ;
16
16
use core:: i32;
@@ -41,7 +41,7 @@ extern mod rustrt {
41
41
pub struct Timespec { sec : i64 , nsec : i32 }
42
42
43
43
impl Timespec {
44
- static fn new( sec: i64 , nsec : i32 ) -> Timespec {
44
+ static pure fn new( sec: i64 , nsec : i32 ) -> Timespec {
45
45
Timespec { sec : sec , nsec : nsec }
46
46
}
47
47
}
@@ -53,6 +53,16 @@ impl Timespec : Eq {
53
53
pure fn ne ( & self , other: & Timespec ) -> bool { !self . eq ( other) }
54
54
}
55
55
56
+ impl Timespec : Ord {
57
+ pure fn lt ( & self , other : & Timespec ) -> bool {
58
+ self . sec < other. sec ||
59
+ ( self . sec == other. sec && self . nsec < other. nsec)
60
+ }
61
+ pure fn le ( & self , other : & Timespec ) -> bool { !other. lt ( self ) }
62
+ pure fn ge ( & self , other : & Timespec ) -> bool { !self . lt ( other) }
63
+ pure fn gt ( & self , other : & Timespec ) -> bool { !self . le ( other) }
64
+ }
65
+
56
66
/**
57
67
* Returns the current time as a `timespec` containing the seconds and
58
68
* nanoseconds since 1970-01-01T00:00:00Z.
@@ -1247,4 +1257,38 @@ mod tests {
1247
1257
assert utc. rfc822z ( ) == ~"Fri , 13 Feb 2009 23 : 31 : 30 -0000 ";
1248
1258
assert utc. rfc3339 ( ) == ~"2009 -02 -13 T23 : 31 : 30 Z ";
1249
1259
}
1260
+
1261
+ #[ test]
1262
+ fn test_timespec_eq_ord ( ) {
1263
+ use core:: cmp:: { eq, ge, gt, le, lt, ne} ;
1264
+
1265
+ let a = & Timespec :: new ( -2 , 1 ) ;
1266
+ let b = & Timespec :: new ( -1 , 2 ) ;
1267
+ let c = & Timespec :: new ( 1 , 2 ) ;
1268
+ let d = & Timespec :: new ( 2 , 1 ) ;
1269
+ let e = & Timespec :: new ( 2 , 1 ) ;
1270
+
1271
+ assert eq( d, e) ;
1272
+ assert ne( c, e) ;
1273
+
1274
+ assert lt( a, b) ;
1275
+ assert lt( b, c) ;
1276
+ assert lt( c, d) ;
1277
+
1278
+ assert le( a, b) ;
1279
+ assert le( b, c) ;
1280
+ assert le( c, d) ;
1281
+ assert le( d, e) ;
1282
+ assert le( e, d) ;
1283
+
1284
+ assert ge( b, a) ;
1285
+ assert ge( c, b) ;
1286
+ assert ge( d, c) ;
1287
+ assert ge( e, d) ;
1288
+ assert ge( d, e) ;
1289
+
1290
+ assert gt( b, a) ;
1291
+ assert gt( c, b) ;
1292
+ assert gt( d, c) ;
1293
+ }
1250
1294
}
0 commit comments