about summary refs log tree commit diff
diff options
context:
space:
mode:
authorClar Charr <clar@charr.xyz>2017-12-04 20:01:31 -0500
committerClar Charr <clar@charr.xyz>2017-12-11 13:38:47 -0500
commit2be7a31d11691103c15b50b06370a8c8b7b26454 (patch)
tree5eb03e36b5cb373a9e118f553681d68cd0a8b240
parent33245fe682874fd138bb6d49b9e5d72e5b915800 (diff)
downloadrust-2be7a31d11691103c15b50b06370a8c8b7b26454.tar.gz
rust-2be7a31d11691103c15b50b06370a8c8b7b26454.zip
Add more Duration methods for consistency.
-rw-r--r--src/libstd/time/duration.rs63
1 files changed, 62 insertions, 1 deletions
diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs
index 86927ce322e..a74e8a21c42 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