From: Jonathan Behrens Date: Sat, 19 May 2018 19:18:07 +0000 (-0400) Subject: Add as_micros and as_millis methods X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=99f5136f9e2df5d60b371e9e19d2f6efe0d2e59e;p=rust.git Add as_micros and as_millis methods --- diff --git a/src/libcore/time.rs b/src/libcore/time.rs index f49917f848a..f43d2db51e7 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -263,18 +263,52 @@ pub fn subsec_micros(&self) -> u32 { self.nanos / NANOS_PER_MICRO } #[inline] pub fn subsec_nanos(&self) -> u32 { self.nanos } + /// Returns the total number of milliseconds contained by this `Duration`. + /// + /// # Examples + /// + /// ``` + /// # #![feature(duration_as_u128)] + /// use std::time::Duration; + /// + /// let duration = Duration::new(5, 730023852); + /// assert_eq!(duration.as_millis(), 5730); + /// ``` + #[unstable(feature = "duration_as_u128", issue = "50202")] + #[inline] + pub fn as_millis(&self) -> u128 { + self.secs as u128 * MILLIS_PER_SEC as u128 + self.nanos as u128 / NANOS_PER_MILLI as u128 + } + + /// Returns the total number of microseconds contained by this `Duration`. + /// + /// # Examples + /// + /// ``` + /// # #![feature(duration_as_u128)] + /// use std::time::Duration; + /// + /// let duration = Duration::new(5, 730023852); + /// assert_eq!(duration.as_micros(), 5730023); + /// ``` + #[unstable(feature = "duration_as_u128", issue = "50202")] + #[inline] + pub fn as_micros(&self) -> u128 { + self.secs as u128 * MICROS_PER_SEC as u128 + self.nanos as u128 / NANOS_PER_MICRO as u128 + } + /// Returns the total number of nanoseconds contained by this `Duration`. /// /// # Examples /// /// ``` - /// # #![feature(duration_nanos)] + /// # #![feature(duration_as_u128)] /// use std::time::Duration; /// /// let duration = Duration::new(5, 730023852); /// assert_eq!(duration.as_nanos(), 5730023852); /// ``` - #[unstable(feature = "duration_nanos", issue = "50202")] + #[unstable(feature = "duration_as_u128", issue = "50202")] #[inline] pub fn as_nanos(&self) -> u128 { self.secs as u128 * NANOS_PER_SEC as u128 + self.nanos as u128