Fix DurationFormatUtils.formatPeriod() calculation when pattern omits 'M' - #1780
Open
Alwaysgaurav1 wants to merge 1 commit into
Open
Fix DurationFormatUtils.formatPeriod() calculation when pattern omits 'M'#1780Alwaysgaurav1 wants to merge 1 commit into
Alwaysgaurav1 wants to merge 1 commit into
Conversation
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.
Description
Fixes a calculation bug in
DurationFormatUtils.formatPeriod(long, long, String, boolean, TimeZone)where duration formatting with patterns containing year (y) and day (d) tokens without month (M) tokens (e.g."y' years 'd' days'"or"y'y 'd'd'") incorrectly inflates the duration by +1 full year (+365 days) when spanning across a calendar year boundary that is less than a full 12-month anniversary.Problem / Reproduction
Prior to this fix:
2024-12-15to2025-01-15) was formatted as"1 years 31 days"(396 days instead of 31 days).2024-01-15to2025-01-10) was formatted as"1 years 26 days"(392 days instead of 361 days).2024-02-29to2025-02-28) was formatted as"1 years 28 days"(393 days instead of 365 days).Root Cause
yearswas initialized asend.get(Calendar.YEAR) - start.get(Calendar.YEAR).Mwas not present intokens, the code rolled month differences intodays, but did not decrementyearswhen less than a full 12-month calendar year had elapsed.startwas not advanced to match the subtractedyears, which caused subsequent month-to-day accumulations to miscount intervening days across year boundaries.Solution
Mis omitted andyis present:months < 0 || (months == 0 && days < 0)). If not, decrementyears.startby the elapsedyears(start.add(Calendar.YEAR, (int) years)).daysuntilstart.get(YEAR) == end.get(YEAR) && start.get(MONTH) == end.get(MONTH).daysfromstart.getActualMaximum(Calendar.DAY_OF_MONTH).Tests Added & Verification
testFormatPeriodWithoutMonths()toDurationFormatUtilsTest.javaverifying: