Skip to content

fix(diff): count months from the start date, not the last step - #480

Merged
spencermountain merged 1 commit into
spencermountain:masterfrom
b84330808:fix-diff-month-clamp
Oct 30, 2026
Merged

spencermountain merged 1 commit into
spencermountain:masterfrom
b84330808:fix-diff-month-clamp

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

diff() counted leftover months by adding one month to the previous result, so after a clamp (jan 31 → feb 28) every later step kept the shorter day:

spacetime('2023-01-31').diff('2023-03-30', 'months') // 2, not 1
spacetime('2008-05-31').diff('2009-05-29', 'months') // 12, not 11 (quarters: 0)

Each step now adds i units to the start date. Tests added in tests/comparisons/diff.test.js; they fail on master and pass with the change.

I used Claude Code to write this change.

diff() counts the leftover months by adding one month at a time to
the previous result. Once a month-end got clamped (jan 31 -> feb 28)
every later step kept the shorter day, so the count ran over:

  spacetime('2023-01-31').diff('2023-03-30', 'months')  // 2, not 1
  spacetime('2008-05-31').diff('2009-05-29', 'months')  // 12, not 11

and quarters, which are derived from months % 12, came out as 0 for
the second one. Add i units to the start date on each step instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@spencermountain

Copy link
Copy Markdown
Owner

good catch! thank you!

@spencermountain
spencermountain merged commit 56e4d3f into spencermountain:master Oct 30, 2026
5 checks passed
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