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

Suggestion: method to get difference between two IsoWeeks #1611

Open
and-reas-se opened this issue Sep 11, 2024 · 1 comment
Open

Suggestion: method to get difference between two IsoWeeks #1611

and-reas-se opened this issue Sep 11, 2024 · 1 comment

Comments

@and-reas-se
Copy link

and-reas-se commented Sep 11, 2024

Hi,

I have a need to figure out how many (ISO) week numbers have passed between two dates. This is an error prone calculation. So if one date is in week 34 and another in week 32 I want 34 - 32 = 2.

  • Idea 1: Take the difference in days between the dates and divide by 7. Does not work because there might be a week boundary between two dates even though less than 7 days have passed.
  • Idea 2: Convert both dates to IsoWeek and subtract the week numbers. This breaks if there is a year boundary between the weeks.
  • Idea 3: Convert both dates to IsoWeek and subtract the week numbers, as well as a fixed multiplier for the number of years in between them. Does not work since a year can have varying numbers of weeks.
  • Idea 4: Convert both dates to IsoWeek, get the years and week numbers for both, use those numbers to generate new dates using NaiveDate::from_isoywd_opt and a fixed day of week, subtract those to get a duration, divide the number of days in that duration by 7 to get elapsed weeks. This works (unless there's another edge case I missed), but this is getting very convoluted for a conceptually simple calculation.

Proposal: Add a method to IsoWeek that compares it to another IsoWeek and returns the number of weeks between them.

@djc
Copy link
Member

djc commented Sep 12, 2024

I'm happy to review a PR for a method for this.

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

No branches or pull requests

2 participants