diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-11-16 17:36:14 -0800 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-11-19 09:32:38 -0800 |
| commit | c6eb8527e09bed702f30ffdc8d6e54acf9b867ca (patch) | |
| tree | d3892a612ffcc125881ae8e4ff04c98ddcd5df49 /src/libstd/sys/common | |
| parent | 22e31f10c22112b486f4999f90e4ba9c7e23b9b6 (diff) | |
| download | rust-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/sys/common')
| -rw-r--r-- | src/libstd/sys/common/mod.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/libstd/sys/common/mod.rs b/src/libstd/sys/common/mod.rs index 44c55d1e2c4..5062be8cd63 100644 --- a/src/libstd/sys/common/mod.rs +++ b/src/libstd/sys/common/mod.rs @@ -98,3 +98,22 @@ pub fn cleanup() { at_exit_imp::cleanup(); }); } + +// Computes (value*numer)/denom without overflow, as long as both +// (numer*denom) and the overall result fit into i64 (which is the case +// for our time conversions). +#[allow(dead_code)] // not used on all platforms +pub fn mul_div_u64(value: u64, numer: u64, denom: u64) -> u64 { + let q = value / denom; + let r = value % denom; + // Decompose value as (value/denom*denom + value%denom), + // substitute into (value*numer)/denom and simplify. + // r < denom, so (denom*numer) is the upper bound of (r*numer) + q * numer + r * numer / denom +} + +#[test] +fn test_muldiv() { + assert_eq!(mul_div_u64( 1_000_000_000_001, 1_000_000_000, 1_000_000), + 1_000_000_000_001_000); +} |
