Repository navigation
fix(rules): keep a leap day from overflowing into March when changing the year - #58
Open
shaurya703 wants to merge 1 commit into
Open
shaurya703 wants to merge 1 commit into
shaurya703 wants to merge 1 commit into
Conversation
… the year Context.Time applied a parsed year while the day was still the one of the reference date. When the reference date is the 29th of February and the target year is not a leap year, time.Date normalizes the day forward, so "in a year" parsed on 2024-02-29 resolved to 2025-03-01 and "a year ago" to 2023-03-01. Clamp the reference day to the last day of the month in the target year, as is already done when the month changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
With a reference date of 2024-02-29:
"in a year"(en.Deadline) resolves to 2025-03-01"a year ago"(en.PastTime) resolves to 2023-03-01Expected 2025-02-28 and 2023-02-28.
Cause
Context.Timeapplies a parsed year withtime.Date(*c.Year, t.Month(), t.Day(), ...)while the day is still the one of the reference date. The 29th of February does not exist in a non-leap year, sotime.Datenormalizes it into March 1st.This is the same overflow that #50 fixed for the month branch; the year branch was not covered.
Fix
Clamp the reference day to the last day of the month in the target year before calling
time.Date, reusing the existingdaysInhelper. Since the fix is inContext.Time, it applies to every language's year rules.Tests
Added
TestDeadlineYearFromLeapDayandTestPastTimeYearFromLeapDayinrules/en. Both fail on master (got 2025-03-01/got 2023-03-01) and pass with the fix. They also check that a leap-to-leap year shift (within 4 years,4 years ago) keeps the 29th.go vet ./...andgo test ./...pass.