-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
43baa2c
Add as_nanos function to duration
fintelia 57b2067
Use NANOS_PER_SEC constant
fintelia dc9e27d
Add issue number
fintelia b66ab1a
Doctest of feature requires that feature
fintelia 799eda4
Issue number should be for tracking issue not PR
fintelia 99f5136
Add as_micros and as_millis methods
fintelia fc89566
Avoid 128-bit arithmetic where possible
fintelia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
|
||
/// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment from |
||
} | ||
|
||
/// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment from |
||
} | ||
|
||
/// Checked `Duration` addition. Computes `self + other`, returning [`None`] | ||
/// if overflow occurred. | ||
/// | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.