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 as_nanos function to Duration #50167

Merged
merged 7 commits into from
Jun 3, 2018
Merged
Changes from 6 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
51 changes: 51 additions & 0 deletions src/libcore/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,57 @@ impl Duration {
#[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
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: self.nanos as u128 / NANOS_PER_MILLI as u128 should be replaced with (self.nanos / NANOS_PER_MILLI) as u128. The former requires doing 128-bit arithmetic on a 32-bit number, which is redundant.

}

/// 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
Copy link
Contributor

Choose a reason for hiding this comment

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

See comment from as_nanos

}

/// Returns the total number of nanoseconds contained by this `Duration`.
///
/// # Examples
///
/// ```
/// # #![feature(duration_as_u128)]
/// use std::time::Duration;
///
/// let duration = Duration::new(5, 730023852);
/// assert_eq!(duration.as_nanos(), 5730023852);
/// ```
#[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
Copy link
Contributor

Choose a reason for hiding this comment

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

See comment from as_nanos

}

/// Checked `Duration` addition. Computes `self + other`, returning [`None`]
/// if overflow occurred.
///
Expand Down