Skip to content

fix(add): walk to the target year when adding years - #479

Merged
spencermountain merged 1 commit into
spencermountain:masterfrom
b84330808:fix-add-year-leap-day
Oct 29, 2026
Merged

spencermountain merged 1 commit into
spencermountain:masterfrom
b84330808:fix-add-year-leap-day

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

add(n, 'year') never set the year itself, so when the leap-day guess was off the year was wrong, and from Feb 29 the clamp used the old leap year:

spacetime('2023-01-01').add(11, 'year')      // 2033-01-01
spacetime('2024-02-29').add(13, 'year')      // 2037-02-25
spacetime('2024-02-29').subtract(13, 'year') // 2011-02-03

This sets the target year on the wanted fields, so walkTo() goes there directly and Feb 29 clamps to the 28th in a common year. Tests added in tests/arithmetic/add.test.js; they fail on master and pass with the change.

I used Claude Code to write this change.

add(n, 'year') moves the epoch by n * 365.05 days, nudges it by a
guessed number of leap days, then walks the month and date back.
The year itself was never set, so when the guess fell short or went
too far the result kept the wrong year:

  spacetime('2023-01-01').add(11, 'year')  // 2033-01-01, not 2034
  spacetime('2040-12-31T22:26').add(2, 'year')  // 2043-12-31, not 2042

From Feb 29 the month-length clamp also used the starting leap year,
so walkTo() was asked for Feb 29 in a common year and gave up
part-way:

  spacetime('2024-02-29').add(13, 'year')  // 2037-02-25, not 2037-02-28

Set the target year on the wanted fields, so the year is walked to
directly and Feb 29 is clamped against the year we land in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@spencermountain
spencermountain merged commit deb3e71 into spencermountain:master Oct 29, 2026
@spencermountain

Copy link
Copy Markdown
Owner

thank you!!

@spencermountain spencermountain mentioned this pull request Nov 6, 2026
Merged
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.

2 participants