summary refs log tree commit diff
path: root/src/libstd/time/duration.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-11-16 17:36:14 -0800
committerAlex Crichton <alex@alexcrichton.com>2015-11-19 09:32:38 -0800
commitc6eb8527e09bed702f30ffdc8d6e54acf9b867ca (patch)
treed3892a612ffcc125881ae8e4ff04c98ddcd5df49 /src/libstd/time/duration.rs
parent22e31f10c22112b486f4999f90e4ba9c7e23b9b6 (diff)
downloadrust-c6eb8527e09bed702f30ffdc8d6e54acf9b867ca.tar.gz
rust-c6eb8527e09bed702f30ffdc8d6e54acf9b867ca.zip
std: Add Instant and SystemTime to std::time
This commit is an implementation of [RFC 1288][rfc] which adds two new unstable
types to the `std::time` module. The `Instant` type is used to represent
measurements of a monotonically increasing clock suitable for measuring time
withing a process for operations such as benchmarks or just the elapsed time to
do something. An `Instant` favors panicking when bugs are found as the bugs are
programmer errors rather than typical errors that can be encountered.

[rfc]: https://github.com/rust-lang/rfcs/pull/1288

The `SystemTime` type is used to represent a system timestamp and is not
monotonic. Very few guarantees are provided about this measurement of the system
clock, but a fixed point in time (`UNIX_EPOCH`) is provided to learn about the
relative distance from this point for any particular time stamp.

This PR takes the same implementation strategy as the `time` crate on crates.io,
namely:

|  Platform  |  Instant                 |  SystemTime              |
|------------|--------------------------|--------------------------|
| Windows    | QueryPerformanceCounter  | GetSystemTimeAsFileTime  |
| OSX        | mach_absolute_time       | gettimeofday             |
| Unix       | CLOCK_MONOTONIC          | CLOCK_REALTIME           |

These implementations can perhaps be refined over time, but they currently
satisfy the requirements of the `Instant` and `SystemTime` types while also
being portable across implementations and revisions of each platform.
Diffstat (limited to 'src/libstd/time/duration.rs')
-rw-r--r--src/libstd/time/duration.rs6
1 files changed, 3 insertions, 3 deletions
diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs
index ca04ec81a28..63d517606a0 100644
--- a/src/libstd/time/duration.rs
+++ b/src/libstd/time/duration.rs
@@ -9,7 +9,7 @@
 // except according to those terms.
 
 use ops::{Add, Sub, Mul, Div};
-use sys::time::SteadyTime;
+use time::Instant;
 
 const NANOS_PER_SEC: u32 = 1_000_000_000;
 const NANOS_PER_MILLI: u32 = 1_000_000;
@@ -67,9 +67,9 @@ impl Duration {
                          abstraction",
                issue = "27799")]
     pub fn span<F>(f: F) -> Duration where F: FnOnce() {
-        let start = SteadyTime::now();
+        let start = Instant::now();
         f();
-        &SteadyTime::now() - &start
+        start.elapsed()
     }
 
     /// Creates a new `Duration` from the specified number of seconds.