about summary refs log tree commit diff
path: root/src/libstd/time.rs
diff options
context:
space:
mode:
authorTim Chevalier <chevalier@alum.wellesley.edu>2013-01-20 14:15:29 -0800
committerTim Chevalier <chevalier@alum.wellesley.edu>2013-01-20 14:15:29 -0800
commitfa4f4fa67321b1a18176fddde4c187ab4b67c056 (patch)
tree935390dd47f3df8c22484c4ee3ec640c1d025921 /src/libstd/time.rs
parentb428cd55b8e1e4b5325b261bc7a474e0e7d8aba6 (diff)
parented17ce1ddae2b8c80a6c97ab32efc15b5e4285b9 (diff)
downloadrust-fa4f4fa67321b1a18176fddde4c187ab4b67c056.tar.gz
rust-fa4f4fa67321b1a18176fddde4c187ab4b67c056.zip
Merge pull request #4519 from cpeterso/impl-timespec-ord
Implement Ord trait for Timespec
Diffstat (limited to 'src/libstd/time.rs')
-rw-r--r--src/libstd/time.rs48
1 files changed, 46 insertions, 2 deletions
diff --git a/src/libstd/time.rs b/src/libstd/time.rs
index d77e02f37b9..e1aea994db1 100644
--- a/src/libstd/time.rs
+++ b/src/libstd/time.rs
@@ -10,7 +10,7 @@
 
 #[forbid(deprecated_mode)];
 
-use core::cmp::Eq;
+use core::cmp::{Eq, Ord};
 use core::int;
 use core::libc::{c_char, c_int, c_long, size_t, time_t};
 use core::i32;
@@ -41,7 +41,7 @@ extern mod rustrt {
 pub struct Timespec { sec: i64, nsec: i32 }
 
 impl Timespec {
-    static fn new(sec: i64, nsec: i32) -> Timespec {
+    static pure fn new(sec: i64, nsec: i32) -> Timespec {
         Timespec { sec: sec, nsec: nsec }
     }
 }
@@ -53,6 +53,16 @@ impl Timespec : Eq {
     pure fn ne(&self, other: &Timespec) -> bool { !self.eq(other) }
 }
 
+impl Timespec : Ord {
+    pure fn lt(&self, other: &Timespec) -> bool {
+        self.sec < other.sec ||
+            (self.sec == other.sec && self.nsec < other.nsec)
+    }
+    pure fn le(&self, other: &Timespec) -> bool { !other.lt(self) }
+    pure fn ge(&self, other: &Timespec) -> bool { !self.lt(other) }
+    pure fn gt(&self, other: &Timespec) -> bool { !self.le(other) }
+}
+
 /**
  * Returns the current time as a `timespec` containing the seconds and
  * nanoseconds since 1970-01-01T00:00:00Z.
@@ -1247,4 +1257,38 @@ mod tests {
         assert utc.rfc822z() == ~"Fri, 13 Feb 2009 23:31:30 -0000";
         assert utc.rfc3339() == ~"2009-02-13T23:31:30Z";
     }
+
+    #[test]
+    fn test_timespec_eq_ord() {
+        use core::cmp::{eq, ge, gt, le, lt, ne};
+
+        let a = &Timespec::new(-2, 1);
+        let b = &Timespec::new(-1, 2);
+        let c = &Timespec::new(1, 2);
+        let d = &Timespec::new(2, 1);
+        let e = &Timespec::new(2, 1);
+
+        assert eq(d, e);
+        assert ne(c, e);
+
+        assert lt(a, b);
+        assert lt(b, c);
+        assert lt(c, d);
+
+        assert le(a, b);
+        assert le(b, c);
+        assert le(c, d);
+        assert le(d, e);
+        assert le(e, d);
+
+        assert ge(b, a);
+        assert ge(c, b);
+        assert ge(d, c);
+        assert ge(e, d);
+        assert ge(d, e);
+
+        assert gt(b, a);
+        assert gt(c, b);
+        assert gt(d, c);
+    }
 }