Repository navigation
Fix the formatting of instants before the epoch (#4399) - #4400
Merged
ramanathan1504 merged 5 commits intoOct 21, 2026
Merged
Conversation
`MutableInstant.initFromEpochMilli` split a negative `epochMilli` with a truncating division, so an instant such as -61500 ms became `epochSecond=-61, nanoOfSecond=-500000000`. `initFromEpochSecond` rejects a negative `nanoOfSecond`, and every consumer expects it in `[0, 10^9)`. Use `Math.floorDiv`, which gives `epochSecond=-62, nanoOfSecond=500000000`. `InstantPatternDynamicFormatter` had the same flaw in two places: it keyed its per-minute cache with `epochSecond / 60` and rendered the seconds with `epochSecond % 60`. For an instant before the epoch this printed negative or garbage characters in the seconds field, for example `23:59:+'.250` instead of `23:59:01.250`, and selected the wrong cached minute. Use `Math.floorDiv` and `Math.floorMod`. Fixes apache#4399
Contributor
|
The |
Per ramanathan1504's review: one InstantPatternDynamicFormatter reused for an instant after the epoch and then one before it. This fails if toEpochMinutes ever reverts to the truncating `/ 60`, since the cached minute would be wrong for the second instant and the formatted output would silently reuse the first instant's seconds.
ramanathan1504
approved these changes
Oct 21, 2026
ramanathan1504
enabled auto-merge (squash)
October 21, 2026 12:34
Contributor
Author
|
Added, thanks — pushed as d5b90a2. Confirmed it catches the regression: reverting toEpochMinutes to the truncating |
ramanathan1504
added a commit
that referenced
this pull request
Nov 9, 2026
Fix the formatting of instants before the epoch (#4400) For instants before 1970, `MutableInstant.initFromEpochMilli` produced a negative nano of second and `%d` printed wrong seconds from a wrong cached minute. Both now use `Math.floorDiv` and `Math.floorMod`, so the output matches `java.time`. Adds tests to `MutableInstantTest` and `InstantPatternDynamicFormatterTest`. --------- Co-authored-by: Ramanathan Muthu <[email protected]> (cherry picked from commit a38a2f5)
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.
Fixes #4399.
Instants before the epoch were mishandled in two places, because a truncating
/or%was used where a floor division is needed:MutableInstant.initFromEpochMilli(long, int)split a negativeepochMilliwith/ 1000, so-61_500 msbecameepochSecond=-61, nanoOfSecond=-500_000_000. That breaks the0 <= nanoOfSecond < 1_000_000_000invariant thatinitFromEpochSecondenforces and every consumer expects. It now usesMath.floorDiv, which givesepochSecond=-62, nanoOfSecond=500_000_000.InstantPatternDynamicFormatterkeyed its per-minute cache withepochSecond / 60and rendered the seconds withepochSecond % 60. Before the epoch the seconds were negative and the wrong cached minute was selected:%d{HH:mm:ss.SSS}printed23:59:+'.000for-59_000 msinstead of23:59:01.000. These now useMath.floorDivandMath.floorMod.Both methods exist since Java 8, so there is no change to the supported baseline. For instants after the epoch the results are identical, because floor and truncating division agree for non-negative operands.
Deliberately not included:
InstantPatternThreadLocalCachedFormatterstarts its cache with the epoch instant-1, so the first event on a thread at exactly second (or millisecond)-1prints an empty string. That is described under "Related" in #4399. I left it out because #4322 rewrites that class.Tests
MutableInstantTest.testInitFromEpochMilliNormalizesTheNanoOfSecond: seven(epochMilli, nanoOfMilli)cases, checking the normalized second and nano and thatgetEpochMillisecond()andgetNanoOfMillisecond()round-trip. Four fail on2.x; the whole-second case and the two cases after the epoch pass before and after.InstantPatternDynamicFormatterTest.output_before_the_epoch_should_match_DateTimeFormatter: six patterns (s,ss,mm:ss,HH:mm:ss.SSS, with micros, with an offset) times eight instants before the epoch, compared withDateTimeFormatteras the existingoutput_should_match_DateTimeFormatterdoes. 42 of the 48 cases fail on2.x; the six that pass are the-60 scases, which fall on a minute boundary.Verification
core.time,core.util.internal.instant,core.util.datetime,core.pattern,core.layoutandcore.impltest packages oflog4j-core-testpass in the default Surefire execution (8,675 tests, 0 failures), and spotless is green on all modules. Thetest-without-jackson-databindexecution of this module cannot load the Jackson-based layout tests that my package filter selects, so I read the results per execution.InstantPatternDynamicFormatterwithDateTimeFormatterover random patterns, zones, locales and instants from year 0 to 9999, about 710,000 comparisons over three seeds. After this change the only differences are zones whose offset has a seconds part (for exampleAfrica/Monroviabefore 1972, whichInstantPatternDynamicFormatterTestalready excludes) and the-1case above../mvnw verifyof the whole reactor locally and rely on CI for it.Generative-AI disclosure (ASF guidance): Claude Code (Anthropic) assisted in finding the problem, drafting the change, tests and this description; the result was reviewed, the new tests were confirmed to fail on
2.xand pass with the change, and the tests and checks above were run before opening this PR.Checklist
2.xbranch if you are targeting Log4j 2; usemainotherwise./mvnw verifysucceeds: not run for the whole reactor locally, see Verification abovesrc/changelog/.2.x.xdirectoryMutableInstantTest: 1 new parameterized test,InstantPatternDynamicFormatterTest: 1 new parameterized test)