about summary refs log tree commit diff
path: root/src/libstd/sys/unix/time.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-04-28 11:40:04 -0700
committerAlex Crichton <alex@alexcrichton.com>2015-05-13 17:50:58 -0700
commit556e76bb78cdd1d951e3966b2264ef8567371881 (patch)
tree2b024aea65f5b77452414fd82a70150b956613c3 /src/libstd/sys/unix/time.rs
parent05d5fcaa5ba0c385e1dc97037c89fae437634fc3 (diff)
downloadrust-556e76bb78cdd1d951e3966b2264ef8567371881.tar.gz
rust-556e76bb78cdd1d951e3966b2264ef8567371881.zip
std: Redesign Duration, implementing RFC 1040
This commit is an implementation of [RFC 1040][rfc] which is a redesign of the
currently-unstable `Duration` type. The API of the type has been scaled back to
be more conservative and it also no longer supports negative durations.

[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1040-duration-reform.md

The inner `duration` module of the `time` module has now been hidden (as
`Duration` is reexported) and the feature name for this type has changed from
`std_misc` to `duration`. All APIs accepting durations have also been audited to
take a more flavorful feature name instead of `std_misc`.

Closes #24874
Diffstat (limited to 'src/libstd/sys/unix/time.rs')
-rw-r--r--src/libstd/sys/unix/time.rs30
1 files changed, 12 insertions, 18 deletions
diff --git a/src/libstd/sys/unix/time.rs b/src/libstd/sys/unix/time.rs
index f59eb2c0301..16dfd3eebd0 100644
--- a/src/libstd/sys/unix/time.rs
+++ b/src/libstd/sys/unix/time.rs
@@ -10,12 +10,15 @@
 
 pub use self::inner::SteadyTime;
 
+const NSEC_PER_SEC: u64 = 1_000_000_000;
+
 #[cfg(any(target_os = "macos", target_os = "ios"))]
 mod inner {
     use libc;
     use time::Duration;
     use ops::Sub;
     use sync::{Once, ONCE_INIT};
+    use super::NSEC_PER_SEC;
 
     pub struct SteadyTime {
         t: u64
@@ -32,11 +35,6 @@ mod inner {
                 t: unsafe { mach_absolute_time() },
             }
         }
-
-        pub fn ns(&self) -> u64 {
-            let info = info();
-            self.t * info.numer as u64 / info.denom as u64
-        }
     }
 
     fn info() -> &'static libc::mach_timebase_info {
@@ -59,8 +57,9 @@ mod inner {
 
         fn sub(self, other: &SteadyTime) -> Duration {
             let info = info();
-            let diff = self.t as i64 - other.t as i64;
-            Duration::nanoseconds(diff * info.numer as i64 / info.denom as i64)
+            let diff = self.t as u64 - other.t as u64;
+            let nanos = diff * info.numer as u64 / info.denom as u64;
+            Duration::new(nanos / NSEC_PER_SEC, (nanos % NSEC_PER_SEC) as u32)
         }
     }
 }
@@ -70,8 +69,7 @@ mod inner {
     use libc;
     use time::Duration;
     use ops::Sub;
-
-    const NSEC_PER_SEC: i64 = 1_000_000_000;
+    use super::NSEC_PER_SEC;
 
     pub struct SteadyTime {
         t: libc::timespec,
@@ -104,10 +102,6 @@ mod inner {
             }
             t
         }
-
-        pub fn ns(&self) -> u64 {
-            self.t.tv_sec as u64 * NSEC_PER_SEC as u64 + self.t.tv_nsec as u64
-        }
     }
 
     impl<'a> Sub for &'a SteadyTime {
@@ -115,12 +109,12 @@ mod inner {
 
         fn sub(self, other: &SteadyTime) -> Duration {
             if self.t.tv_nsec >= other.t.tv_nsec {
-                Duration::seconds(self.t.tv_sec as i64 - other.t.tv_sec as i64) +
-                    Duration::nanoseconds(self.t.tv_nsec as i64 - other.t.tv_nsec as i64)
+                Duration::new(self.t.tv_sec as u64 - other.t.tv_sec as u64,
+                              self.t.tv_nsec as u32 - other.t.tv_nsec as u32)
             } else {
-                Duration::seconds(self.t.tv_sec as i64 - 1 - other.t.tv_sec as i64) +
-                    Duration::nanoseconds(self.t.tv_nsec as i64 + NSEC_PER_SEC -
-                                          other.t.tv_nsec as i64)
+                Duration::new(self.t.tv_sec as u64 - 1 - other.t.tv_sec as u64,
+                              self.t.tv_nsec as u32 + (NSEC_PER_SEC as u32) -
+                                          other.t.tv_nsec as u32)
             }
         }
     }