-
Notifications
You must be signed in to change notification settings - Fork 544
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
Create the subsec_millis and subsec_micros methods for Duration #1351
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -221,7 +221,9 @@ impl Duration { | |
} | ||
} | ||
|
||
/// Returns the number of nanoseconds such that | ||
/// Returns the number of nanoseconds in the fractional part of the duration. | ||
/// | ||
/// This is the number of nanoseconds such that | ||
/// `subsec_nanos() + num_seconds() * NANOS_PER_SEC` is the total number of | ||
/// nanoseconds in the duration. | ||
pub const fn subsec_nanos(&self) -> i32 { | ||
|
@@ -232,6 +234,24 @@ impl Duration { | |
} | ||
} | ||
|
||
/// Returns the number of microseconds in the fractional part of the duration. | ||
/// | ||
/// This is the number of microseconds such that | ||
/// `subsec_micros() + num_seconds() * MICROS_PER_SEC` is the total number of | ||
botahamec marked this conversation as resolved.
Show resolved
Hide resolved
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. @pitdicker same question as below regarding |
||
/// microseconds in the duration. | ||
pub const fn subsec_micros(&self) -> i32 { | ||
self.subsec_nanos() / NANOS_PER_MICRO | ||
} | ||
|
||
/// Returns the number of milliseconds in the fractional part of the duration. | ||
/// | ||
/// This is the number of milliseconds such that | ||
/// `subsec_millis() + num_seconds() * MILLIS_PER_SEC` is the total number of | ||
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. I like this docstring specificity. @pitdicker should Stretch goal would be to make all the constants in that clump of constants above into 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. I think it makes sense to make these constants part of the public API. For these docstrings, I'd like to keep a shorter first line, more like 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. A made a change like that to the docstrings. I'm wondering if those constants should stay in the 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. Makes sense to me! 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. Just made that change, but I do have one more question. Now that these are public, I find it a bit strange that some of them are 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.
I believe the goal for using |
||
/// milliseconds in the duration. | ||
pub const fn subsec_millis(&self) -> i32 { | ||
self.subsec_nanos() / NANOS_PER_MILLI | ||
} | ||
|
||
/// Returns the total number of whole milliseconds in the duration, | ||
pub const fn num_milliseconds(&self) -> i64 { | ||
// A proper Duration will not overflow, because MIN and MAX are defined | ||
|
@@ -647,6 +667,33 @@ mod tests { | |
assert_eq!(Duration::days(-i64::MAX / NANOS_PER_DAY - 1).num_nanoseconds(), None); | ||
} | ||
|
||
#[test] | ||
fn test_duration_subsec_nanos() { | ||
assert_eq!(Duration::zero().subsec_nanos(), 0); | ||
assert_eq!(Duration::nanoseconds(1).subsec_nanos(), 1); | ||
assert_eq!(Duration::nanoseconds(-1).subsec_nanos(), -1); | ||
assert_eq!(Duration::seconds(1).subsec_nanos(), 0); | ||
assert_eq!(Duration::nanoseconds(1_000_000_001).subsec_nanos(), 1); | ||
} | ||
|
||
#[test] | ||
fn test_duration_subsec_micros() { | ||
assert_eq!(Duration::zero().subsec_micros(), 0); | ||
assert_eq!(Duration::microseconds(1).subsec_micros(), 1); | ||
assert_eq!(Duration::microseconds(-1).subsec_micros(), -1); | ||
assert_eq!(Duration::seconds(1).subsec_micros(), 0); | ||
assert_eq!(Duration::microseconds(1_000_001).subsec_micros(), 1); | ||
} | ||
|
||
#[test] | ||
fn test_duration_subsec_millis() { | ||
assert_eq!(Duration::zero().subsec_millis(), 0); | ||
assert_eq!(Duration::milliseconds(1).subsec_millis(), 1); | ||
assert_eq!(Duration::milliseconds(-1).subsec_millis(), -1); | ||
assert_eq!(Duration::seconds(1).subsec_millis(), 0); | ||
assert_eq!(Duration::milliseconds(1_000_001).subsec_millis(), 1); | ||
} | ||
|
||
#[test] | ||
fn test_duration_checked_ops() { | ||
assert_eq!( | ||
|
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.
These improvements for the new methods should be introduced in the same commit as the methods themselves, as I explained in my previous comment. Only the changes for existing methods should remain in this commit.