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

Use chrono::{Months,Days} for correct calculation #28

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

simon04
Copy link

@simon04 simon04 commented Jul 30, 2023

The duration (amount of seconds) of a year, month, week, days depends on the reference date.

Introduces add_relative_str to add the relative duration to the given DateTime instance.

Fixes uutils/coreutils#4811.

The duration (amount of seconds) of a year, month, week, days depends on the reference date.

Introduces `add_relative_str` to add the relative duration to the given DateTime instance.
Copy link
Member

@tertsdiepraam tertsdiepraam left a comment

Choose a reason for hiding this comment

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

Too bad chrono doesn't have some more general Duration type for this. Once this gets more complex we might need to make one ourselves. This looks like a pretty good workaround in the meantime.

Also, @sylvestre Looks like I don't have the rights to approve the CI here :)

@sylvestre
Copy link
Contributor

@simon04 sorry for the latency, i was on holidays

@tertsdiepraam permissions updated

Copy link
Member

@tertsdiepraam tertsdiepraam left a comment

Choose a reason for hiding this comment

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

Alright, let's get back to this. I think this is an important and good fix, so I want to get it merged, but we need to take some other changes into account.

src/lib.rs Outdated
Comment on lines 123 to 128
pub fn from_str_at_date(date: NaiveDate, s: &str) -> Result<Duration, ParseDurationError> {
let time_now = Local::now()
.date_naive()
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc();
Copy link
Member

Choose a reason for hiding this comment

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

I think the most sensible thing to do is to say that this function should always accept some reference DateTime<Tz> instead of a naive date. That's because this function needs to be timezone-aware as well. Using Local specifically makes this harder to test and leads to issues like #36. Maybe that makes splitting off add_relative_str into a separate function unnecessary?

This also ties in with #33, because we should be focusing on returning DateTimes, not Durations. So maybe the add_relative_str is actually all we need? What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

I kept from_str_at_date for backwards compatibility. How do you deal with breaking changes in this library?

I agree, any function returning a Duration should be deprecated.

Copy link
Member

Choose a reason for hiding this comment

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

Since the library is very new, I think we should make all these changes sooner rather than later. Once the library settles a bit more we can think about stronger stability guarantees.

Copy link
Author

Choose a reason for hiding this comment

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

Alright, I'll remove from_str and from_str_at_date, and adapt the unit tests to call add_relative_str instead...

Copy link
Author

Choose a reason for hiding this comment

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

Done. Please review. Thanks!

src/lib.rs Outdated Show resolved Hide resolved
The duration (amount of seconds) of a year, month, week, days depends on the reference date and timezone.

Thus we only provide the unambiguous `add_relative_str`.
@sylvestre
Copy link
Contributor

sorry but could you please rebase it ? :(

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.

date: --date=month and --date=year return wrong date and time
3 participants