Skip to content

Count whole calendar years in elapsedTime - #401

Open
Shriraj1901 wants to merge 1 commit into
github:mainfrom
Shriraj1901:count-calendar-years-in-elapsed-time
Open

Shriraj1901 wants to merge 1 commit into
github:mainfrom
Shriraj1901:count-calendar-years-in-elapsed-time

Conversation

@Shriraj1901

Copy link
Copy Markdown

elapsedTime splits a duration using 30 day months, which makes a year 360 days long. For long durations that drift adds up and the rounded result lands in the wrong year, e.g. 2020-01-01 to 2025-01-01 was shown as "6 years ago" and 2015-02-14 to 2025-02-06 as "11 years ago".

Count the whole calendar years first and only approximate the remainder, so durations shorter than a year are unchanged. This also removes the FIXME about this bug in the micro format tests.

Fixes #307

elapsedTime splits a duration using 30 day months, which makes a year 360
days long. For long durations that drift adds up and the rounded result
lands in the wrong year, e.g. 2020-01-01 to 2025-01-01 was shown as
"6 years ago" and 2015-02-14 to 2025-02-06 as "11 years ago".

Count the whole calendar years first and only approximate the remainder,
so durations shorter than a year are unchanged. This also removes the
FIXME about this bug in the micro format tests.

Fixes github#307
@Shriraj1901
Shriraj1901 requested a review from a team as a code owner November 9, 2026 03:22
Copilot AI auto review requested due to automatic review settings November 9, 2026 03:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The duration calculation still mishandles near-anniversary remainders, producing “6 years ago” for an interval shorter than five years.

1 open issue
What changed in this PR

This PR addresses #307 by counting whole calendar years before approximating the remaining duration, reducing drift in long-range relative-time displays.

Changes:

  • Updates elapsedTime to subtract calendar years before decomposing the remainder.
  • Adds regression tests for long past and future intervals.
  • Updates duration expectations and removes obsolete FIXME comments.
File Description
test/​relative-time.js Adds long-range display regressions and updates expected durations.
test/​duration.ts Adds elapsed-duration cases for multi-year intervals.
src/​duration.ts Counts calendar years before approximating remaining time.

Review effort: Balanced (auto)

Comment thread src/duration.ts
Comment on lines +167 to +168
const extraYears = Math.floor(month / 12)
const year = Math.max(calendarYears, 0) + extraYears

This branch has not been deployed

No deployments
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.

One full year extra when displaying relative time to 2015

2 participants