From 2be7a31d11691103c15b50b06370a8c8b7b26454 Mon Sep 17 00:00:00 2001 From: Clar Charr Date: Mon, 4 Dec 2017 20:01:31 -0500 Subject: [PATCH] Add more Duration methods for consistency. --- src/libstd/time/duration.rs | 63 ++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs index 86927ce322eae..a74e8a21c42a6 100644 --- a/src/libstd/time/duration.rs +++ b/src/libstd/time/duration.rs @@ -108,7 +108,7 @@ impl Duration { /// let duration = Duration::from_millis(2569); /// /// assert_eq!(2, duration.as_secs()); - /// assert_eq!(569000000, duration.subsec_nanos()); + /// assert_eq!(569_000_000, duration.subsec_nanos()); /// ``` #[stable(feature = "duration", since = "1.3.0")] #[inline] @@ -139,6 +139,27 @@ impl Duration { Duration { secs: secs, nanos: nanos } } + /// Creates a new `Duration` from the specified number of nanoseconds. + /// + /// # Examples + /// + /// ``` + /// #![feature(duration_extras)] + /// use std::time::Duration; + /// + /// let duration = Duration::from_nanos(1_000_000_123); + /// + /// assert_eq!(1, duration.as_secs()); + /// assert_eq!(123, duration.subsec_nanos()); + /// ``` + #[unstable(feature = "duration_extras", issue = "46507")] + #[inline] + pub fn from_nanos(nanos: u64) -> Duration { + let secs = nanos / (NANOS_PER_SEC as u64); + let nanos = (nanos % (NANOS_PER_SEC as u64)) as u32; + 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 @@ -171,6 +192,46 @@ impl Duration { #[inline] pub fn as_secs(&self) -> u64 { self.secs } + /// Returns the fractional part of this `Duration`, in milliseconds. + /// + /// This method does **not** return the length of the duration when + /// represented by milliseconds. The returned number always represents a + /// fractional portion of a second (i.e. it is less than one thousand). + /// + /// # Examples + /// + /// ``` + /// #![feature(duration_extras)] + /// use std::time::Duration; + /// + /// let duration = Duration::from_millis(5432); + /// assert_eq!(duration.as_secs(), 5); + /// assert_eq!(duration.subsec_nanos(), 432_000_000); + /// ``` + #[unstable(feature = "duration_extras", issue = "46507")] + #[inline] + pub fn subsec_millis(&self) -> u32 { self.nanos / NANOS_PER_MILLI } + + /// Returns the fractional part of this `Duration`, in microseconds. + /// + /// This method does **not** return the length of the duration when + /// represented by microseconds. The returned number always represents a + /// fractional portion of a second (i.e. it is less than one million). + /// + /// # Examples + /// + /// ``` + /// #![feature(duration_extras, duration_from_micros)] + /// use std::time::Duration; + /// + /// let duration = Duration::from_micros(1_234_567); + /// assert_eq!(duration.as_secs(), 1); + /// assert_eq!(duration.subsec_nanos(), 234_567_000); + /// ``` + #[unstable(feature = "duration_extras", issue = "46507")] + #[inline] + pub fn subsec_micros(&self) -> u32 { self.nanos / NANOS_PER_MICRO } + /// Returns the fractional part of this `Duration`, in nanoseconds. /// /// This method does **not** return the length of the duration when