Skip to content

Commit

Permalink
Auto merge of #46508 - clarcharr:duration_extras, r=sfackler
Browse files Browse the repository at this point in the history
Add more Duration methods for consistency.

Follow-up to #46507.
  • Loading branch information
bors committed Dec 20, 2017
2 parents 6dbf0ba + 2be7a31 commit 16212b9
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion src/libstd/time/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 16212b9

Please sign in to comment.