Repository navigation
Count whole calendar years in elapsedTime - #401
Open
Shriraj1901 wants to merge 1 commit into
Open
Shriraj1901 wants to merge 1 commit into
Shriraj1901 wants to merge 1 commit into
Conversation
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
Contributor
There was a problem hiding this comment.
🟡 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
elapsedTimeto 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 on lines
+167
to
+168
| const extraYears = Math.floor(month / 12) | ||
| const year = Math.max(calendarYears, 0) + extraYears |
This branch has not been deployed
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.

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