fix: duration humanize ignores leap years (fixed-unit decomposition) - #3172
Open
Jerryyy985 wants to merge 1 commit into
Open
fix: duration humanize ignores leap years (fixed-unit decomposition)#3172Jerryyy985 wants to merge 1 commit into
Jerryyy985 wants to merge 1 commit into
Conversation
humanize() added the fixed-365-day $ms to a real calendar date and diffed it, so durations computed with fixed units drifted with the calendar (P9999Y -> '9992 years', and results varied with the current date). Fix: decompose $ms into fixed units (MILLISECONDS_A_YEAR etc., same basis as asYears) and format via the relativeTime locale templates, reproducing the threshold chain exactly. P9999Y now renders '9999 years'; month/year results are date-independent and match moment.js. Fixes iamkun#3170
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.
Summary
humanize()added the fixed-365-day$msto a real calendar date and diffed it viafromNow, so durations computed with fixed units drifted with the calendar —dayjs.duration('P9999Y').humanize()rendered'9992 years', and results varied with the current date (leap-year position).Fix
Decompose
$msinto fixed units (MILLISECONDS_A_YEARetc., the same basis asasYears) and format through the relativeTime locale templates, reproducing the threshold chain exactly (including the'M'no-d field andabs<=1fallback).P9999Y→'9999 years'(was'9992 years')P18M→'2 years')Fixes #3170