diff options
| author | bors <bors@rust-lang.org> | 2017-09-23 22:21:32 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-09-23 22:21:32 +0000 |
| commit | 24831c7221c7b4e32a02f8e8cffe7dfacb88b520 (patch) | |
| tree | 5783bd8009b1bd403fb7bd111d6a626a281d32e5 /src/libstd | |
| parent | 26015da01497b4014fc4f2ecedee5a7090c354e6 (diff) | |
| parent | b40a9f4ecac2d0da2c96576e63e456a868026245 (diff) | |
| download | rust-24831c7221c7b4e32a02f8e8cffe7dfacb88b520.tar.gz rust-24831c7221c7b4e32a02f8e8cffe7dfacb88b520.zip | |
Auto merge of #44436 - MicroJoe:master, r=alexcrichton
Add Duration::from_micros This fixes #44400 that explains why it could be useful for embedded designs timing.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/time/duration.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs index d715a0d740b..86927ce322e 100644 --- a/src/libstd/time/duration.rs +++ b/src/libstd/time/duration.rs @@ -13,7 +13,9 @@ use ops::{Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign}; const NANOS_PER_SEC: u32 = 1_000_000_000; const NANOS_PER_MILLI: u32 = 1_000_000; +const NANOS_PER_MICRO: u32 = 1_000; const MILLIS_PER_SEC: u64 = 1_000; +const MICROS_PER_SEC: u64 = 1_000_000; /// A `Duration` type to represent a span of time, typically used for system /// timeouts. @@ -116,6 +118,27 @@ impl Duration { Duration { secs: secs, nanos: nanos } } + /// Creates a new `Duration` from the specified number of microseconds. + /// + /// # Examples + /// + /// ``` + /// #![feature(duration_from_micros)] + /// use std::time::Duration; + /// + /// let duration = Duration::from_micros(1_000_002); + /// + /// assert_eq!(1, duration.as_secs()); + /// assert_eq!(2000, duration.subsec_nanos()); + /// ``` + #[unstable(feature = "duration_from_micros", issue = "44400")] + #[inline] + pub fn from_micros(micros: u64) -> Duration { + let secs = micros / MICROS_PER_SEC; + let nanos = ((micros % MICROS_PER_SEC) as u32) * NANOS_PER_MICRO; + Duration { secs: secs, nanos: nanos } + } + /// Returns the number of _whole_ seconds contained by this `Duration`. /// /// The returned value does not include the fractional (nanosecond) part of the |
