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

Morph elapsed_years() API into years_since() #707

Merged
merged 1 commit into from Jun 8, 2022
Merged

Morph elapsed_years() API into years_since() #707

merged 1 commit into from Jun 8, 2022

Conversation

djc
Copy link
Contributor

@djc djc commented Jun 8, 2022

@yozhgoor would you like to review? This:

  • Is more flexible, since it allows you to provide both dates for the interval
  • Is more precise, since it returns None if the base is in the future
  • Doesn't have to be guarded with [cfg(feature = "clock")] since it doesn't rely on Utc::today()
  • Does not expose imprecise new constants as part of the public API

Copy link
Contributor

@cecton cecton left a comment

Choose a reason for hiding this comment

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

tbh I think it's complementary.

It doesn't cost much to implement both because elapsed_years can call years_since.

src/date.rs Show resolved Hide resolved
Comment on lines -511 to -523
/// Constants that can be used by all components.
pub mod consts {
/// Constants of type `f32`.
pub mod f32 {
/// Number of weeks in a year.
pub const WEEKS_PER_YEAR: f32 = 52.1775;
}
/// Constants of type `f64`.
pub mod f64 {
/// Number of weeks in a year.
pub const WEEKS_PER_YEAR: f64 = 52.1775;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not "imprecise". This actually very precise: https://en.wikipedia.org/wiki/Week#Definition_and_duration

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, if you define a YEAR to be a "mean year" computed across 400 years, then yes. For the conventional meaning of a year (of either 365 or 366 days) it isn't very precise.

Copy link
Contributor

Choose a reason for hiding this comment

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

So I guess it's just the naming that is incorrect

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure to understand, 365.2425 days is more precise than either 365 or 366 days.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All other things being equal I'd prefer not to expose relatively niche API surface.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not like the number of weeks in a mean year is going to change though

Comment on lines 251 to 253
let one_year_ago = Utc::today() - Duration::weeks((WEEKS_PER_YEAR * 1.5).ceil() as i64);
// A bit more than 2 years.
let two_year_ago = Utc::today() - Duration::weeks((WEEKS_PER_YEAR * 2.5).ceil() as i64);
Copy link
Contributor

Choose a reason for hiding this comment

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

You should probably not use today here but give a specific date. This test is tricky to get it right. @yozhgoor did it right but it isn't easy to understand why.

If you give a specific date, it will be right all the time and it's easier to understand.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean: the test success depends on the exact date you are running the test. If you get it wrong you can easily get a flaky test that breaks sometimes every 4 years or so.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The tests I had here (which I had moved around due to some confusion) are direct ports of the original tests to the new API), so I don't think this is different from before?

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean the test is right and will succeed but it depends on an external variable that is the current date and it is hard to read and understand why it will always succeed.

It's safer and easier to use a specific date (take the current date for instance) so it doesn't vary each time the test is ran.

So my point is not about the test being wrong or right but the maintainability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah sorry, I thought you were implying I had regressed this test compared to the previous version.

I'm honestly not sure if I agree that using a specific date would be safer and easier. Technically, yes, some non-deterministic behavior is introduced by relying on the current date, but given the constants used I feel these tests are pretty robust and the maintainability doesn't suffer enough for me to go and rewrite them.

@djc
Copy link
Contributor Author

djc commented Jun 8, 2022

I agree to some extent that they're complementary. At the same time, I'd argue that both are relatively niche (if only for the fact that we've managed to get by without them for many years) and, given that I'm here as a replacement maintainer trying to get a handle on maintenance I'd prefer to have less public API, so I'm inclined to stick with only the more powerful API for now.

(TBH if someone were to submit a PR adding back the other one with some test coverage, I'd probably merge it.)

@cecton
Copy link
Contributor

cecton commented Jun 8, 2022

(TBH if someone were to submit a PR adding back the other one with some test coverage, I'd probably merge it.)

Since @yozhgoor originally made the API for elapsed_years instead of years_since despite it would have been easier, I expect that he would probably benefit from it.

@djc djc merged commit 752e69a into main Jun 8, 2022
@djc djc deleted the years-since branch June 8, 2022 12:14
@djc
Copy link
Contributor Author

djc commented Jun 8, 2022

Thanks for the feedback!

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.

None yet

3 participants