Skip to content

fix(rules): keep a leap day from overflowing into March when changing the year - #58

Open
shaurya703 wants to merge 1 commit into
olebedev:masterfrom
shaurya703:fix/year-leap-day-overflow
Open

shaurya703 wants to merge 1 commit into
olebedev:masterfrom
shaurya703:fix/year-leap-day-overflow

Conversation

@shaurya703

Copy link
Copy Markdown

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-01

Expected 2025-02-28 and 2023-02-28.

Cause

Context.Time applies a parsed year with time.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, so time.Date normalizes 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 existing daysIn helper. Since the fix is in Context.Time, it applies to every language's year rules.

Tests

Added TestDeadlineYearFromLeapDay and TestPastTimeYearFromLeapDay in rules/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 ./... and go test ./... pass.

… 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.
@shaurya703
shaurya703 requested a review from olebedev as a code owner October 26, 2026 08:21
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