Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more Duration methods for consistency. #46508

Merged
merged 1 commit into from
Dec 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like it should be from_nanosecs or something like that by the convention in other methods.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing constructors are new, from_secs, from_millis, and from_micros, so it seems like from_nanos would match conventions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we've been somewhat inconsistent with abbreviating seconds already, so this seems fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally like the use of the pluralised prefices, considering how simply using ns, us, and ms would be very easy to make typos. I don't personally like using sec as an abbreviation for second considering how sec is secant and s is second, but that's just because I'm a maths person and I appreciate clarity in equations. We shouldn't really change the naming scheme now that this has been stabilised for a long time.

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