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

Create the subsec_millis and subsec_micros methods for Duration #1351

Closed
wants to merge 3 commits into from

Conversation

botahamec
Copy link
Contributor

Fixes #1348

Copy link

codecov bot commented Nov 4, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (15c8885) 91.61% compared to head (10cd25f) 91.63%.
Report is 3 commits behind head on 0.4.x.

Additional details and impacted files
@@            Coverage Diff             @@
##            0.4.x    #1351      +/-   ##
==========================================
+ Coverage   91.61%   91.63%   +0.01%     
==========================================
  Files          38       38              
  Lines       17482    17519      +37     
==========================================
+ Hits        16016    16053      +37     
  Misses       1466     1466              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

src/duration.rs Show resolved Hide resolved
src/duration.rs Outdated Show resolved Hide resolved
}

/// Returns the number of milliseconds such that
/// `subsec_millis() + num_seconds() * MILLIS_PER_SEC` is the total number of
Copy link
Contributor

@jtmoon79 jtmoon79 Nov 4, 2023

Choose a reason for hiding this comment

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

I like this docstring specificity.

@pitdicker should MILLIS_PER_SEC be made pub? If a user wants to follow this docstring implied suggestion (compute subsec_millis() + num_seconds() * MILLIS_PER_SEC) then they'll want to access MILLIS_PER_SEC. This docstring is taken from subsec_nanos docstring which has the same implied suggestion but using NANOS_PER_SEC, so NANOS_PER_SEC should probably also be pub.

Stretch goal would be to make all the constants in that clump of constants above into pub.

Copy link
Contributor

@djc djc Nov 6, 2023

Choose a reason for hiding this comment

The 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 core (which has "Returns the fractional part of this Duration, in whole microseconds."). We can still show this formula, but it shouldn't be in the first line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 duration module. They might make more sense if they were put in the lib.rs. I can't imagine anyone looking for these constants intuiting that they'd be in duration.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense to me!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 i32 and some are i64. I think this makes sense for what we're using them for, and if it's useful for us to have them that way, then it's probably useful for someone else too, but I wanted to bring it in case someone thinks we should make it more consistent.

Copy link
Contributor

@jtmoon79 jtmoon79 Nov 16, 2023

Choose a reason for hiding this comment

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

I find it a bit strange that some of them are i32 and some are i64

I believe the goal for using i32 is to lessen resource "footprint" chrono objects.

@@ -232,6 +232,20 @@ impl Duration {
}
}

/// Returns the number of microseconds such that
/// `subsec_micros() + num_seconds() * MICROS_PER_SEC` is the total number of
Copy link
Contributor

Choose a reason for hiding this comment

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

@pitdicker same question as below regarding pub MICROS_PER_SEC.

Copy link
Contributor

@djc djc left a comment

Choose a reason for hiding this comment

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

I'd like this split into clean commits.

  • One for moving the constants (and making them pub)
  • One for adding new methods (with complete docs and tests)
  • One for improving docs on current methods

Copy link
Contributor

@jtmoon79 jtmoon79 left a comment

Choose a reason for hiding this comment

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

LGTM with nitpicks about inconsistent docstring

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
@botahamec
Copy link
Contributor Author

Sorry I took a while
@jtmoon79 I addressed your comments and rebased to make my commits match @djc's suggestion

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
@@ -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.
Copy link
Contributor

@djc djc Nov 20, 2023

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.

/// The number of nanoseconds in a second.
pub const NANOS_PER_SEC: i32 = 1_000_000_000;
/// The number of microseconds in a second.
pub const MICROS_PER_SEC: i64 = 1_000_000;
Copy link
Collaborator

@pitdicker pitdicker Jan 30, 2024

Choose a reason for hiding this comment

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

The types of these constants are 'whatever needs the fewest casts in our current code'.

That is: everything that has to do with seconds or more is using the type of the Duration.secs field.
Everything that has to do with subsecond units uses the type of the Duration.nanos field.
For most constants that seems like a good default.

NANOS_PER_SEC and MICROS_PER_SEC are at the boundary. NANOS_PER_SEC is currently an i32, and we sometimes cast it to an i64.

Maybe we should change MICROS_PER_SEC to also be an i32. Argument is consistency with NANOS_PER_SEC, and that widening casts are infallible and available with a From impl (even though this is just a constant and would not fail anyway).

@pitdicker
Copy link
Collaborator

@botahamec Do you want to push this PR over the finish line? Please adjust the commits a bit more matching @djc's request, and adjust the type of MICROS_PER_SEC.

Note that we will hopefully merge two PRs the next few days that touch surrounding code: #1337 and #1385. You may want to wait with rebasing until that is done.

@pitdicker
Copy link
Collaborator

Note that we will hopefully merge two PRs the next few days that touch surrounding code: #1337 and #1385. You may want to wait with rebasing until that is done.

They are merged now.

@pitdicker
Copy link
Collaborator

@botahamec Can you let us now if you are still interested in working on this PR?

@pitdicker
Copy link
Collaborator

Closing due to inactivity.

@pitdicker pitdicker closed this Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add subsec methods to Duration
4 participants