-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Add Duration::from_micros #44436
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
Merged
Merged
Add Duration::from_micros #44436
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4db499f
Add Duration::from_micros
gagath 376837b
from_micros: Inlined return for consistency
gagath 2e6aed8
from_micros: Added unstable annotation
gagath abc53cc
from_micros: Fix missing {
gagath a15c541
from_micros: added issue number and fixed typo
gagath b40a9f4
from_micros: add feature name
gagath 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 hidden or 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 |
---|---|---|
|
@@ -13,7 +13,9 @@ use ops::{Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign}; | |
|
||
const NANOS_PER_SEC: u32 = 1_000_000_000; | ||
const NANOS_PER_MILLI: u32 = 1_000_000; | ||
const NANOS_PER_MICROS: u32 = 1_000; | ||
const MILLIS_PER_SEC: u64 = 1_000; | ||
const MICROS_PER_SEC: u64 = 1_000_000; | ||
|
||
/// A `Duration` type to represent a span of time, typically used for system | ||
/// timeouts. | ||
|
@@ -114,6 +116,25 @@ impl Duration { | |
let secs = millis / MILLIS_PER_SEC; | ||
let nanos = ((millis % MILLIS_PER_SEC) as u32) * NANOS_PER_MILLI; | ||
Duration { secs: secs, nanos: nanos } | ||
|
||
/// Creates a new `Duration` from the specified number of microseconds. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
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. This test will need to enable that feature to build properly. |
||
/// use std::time::Duration; | ||
/// | ||
/// let duration = Duration::from_micros(1_000_002); | ||
/// | ||
/// assert_eq!(1, duration.as_secs()); | ||
/// assert_eq!(2000, duration.subsec_nanos()); | ||
/// ``` | ||
#[unstable(feature = "", issue = "")] | ||
#[inline] | ||
pub fn from_micros(micros: u64) -> Duration { | ||
let secs = micros / MICROS_PER_SEC; | ||
let nanos = ((micros % MICROS_PER_SEC) as u32) * NANOS_PER_MICRO; | ||
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. Typo. Also, the [00:02:49] error[E0425]: cannot find value `NANOS_PER_MICRO` in this scope
[00:02:49] --> /checkout/src/libstd/time/duration.rs:137:58
[00:02:49] |
[00:02:49] 137 | let nanos = ((micros % MICROS_PER_SEC) as u32) * NANOS_PER_MICRO;
[00:02:49] | ^^^^^^^^^^^^^^^ did you mean `NANOS_PER_MICROS`?
[00:02:49]
[00:02:49] error[E0545]: incorrect 'issue'
[00:02:49] --> /checkout/src/libstd/time/duration.rs:133:5
[00:02:49] |
[00:02:49] 133 | #[unstable(feature = "", issue = "")]
[00:02:49] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[00:02:49]
[00:02:51] error: aborting due to 2 previous errors
[00:02:51]
[00:02:51] error: Could not compile `std`. |
||
Duration { secs: secs, nanos: nanos } | ||
} | ||
|
||
/// Returns the number of _whole_ seconds contained by this `Duration`. | ||
|
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.
You missed a
}
here.