Skip to content

compare week-year not calendar year in compareWeeks#408

Open
sahvx655-wq wants to merge 1 commit into
apache:masterfrom
sahvx655-wq:calendar-week-year-compare
Open

compare week-year not calendar year in compareWeeks#408
sahvx655-wq wants to merge 1 commit into
apache:masterfrom
sahvx655-wq:calendar-week-year-compare

Conversation

@sahvx655-wq

Copy link
Copy Markdown
Contributor

compareWeeks reports 31 December and 1 January of the same calendar year as the same week. AbstractCalendarValidator.compare orders the WEEK_OF_YEAR field on Calendar.YEAR and then WEEK_OF_YEAR, but a week belongs to its own week-year, and at the turn of the year that week-year differs from the calendar year. Under the US locale 31 Dec 2018 has WEEK_OF_YEAR 1 and a week-year of 2019, so against 1 Jan 2018 (also WEEK_OF_YEAR 1) the year and the week number both match and two dates roughly 52 weeks apart compare equal. The mirror case fails the other way: 31 Dec 2018 and 1 Jan 2019 share week 1 of week-year 2019 yet compare as different weeks because their calendar years differ.

The fix orders the WEEK_OF_YEAR comparison on the week-year from Calendar.getWeekYear(), falling back to Calendar.YEAR where the calendar does not support week dates, before comparing the week number, and leaves every other field untouched. compareQuarters already shifts its year at the quarter boundary, so this keeps the week comparison consistent with it. The added test fails on the current code and passes with the change.

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute?
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

AbstractCalendarValidator.compare ordered WEEK_OF_YEAR on Calendar.YEAR, but a week belongs to its week-year, which differs from the calendar year at the year boundary, so compareWeeks treated 31 Dec and 1 Jan of the same calendar year as the same week. Compare the week-year (getWeekYear, falling back to YEAR when week dates are unsupported) before the week number.
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.

1 participant