Skip to content

Fix the formatting of instants before the epoch (#4399) - #4400

Merged
ramanathan1504 merged 5 commits into
apache:2.xfrom
sharanggupta:fix-negative-epoch-instant
Oct 21, 2026
Merged

ramanathan1504 merged 5 commits into
apache:2.xfrom
sharanggupta:fix-negative-epoch-instant

Conversation

@sharanggupta

Copy link
Copy Markdown
Contributor

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 negative epochMilli with / 1000, so -61_500 ms became epochSecond=-61, nanoOfSecond=-500_000_000. That breaks the 0 <= nanoOfSecond < 1_000_000_000 invariant that initFromEpochSecond enforces and every consumer expects. It now uses Math.floorDiv, which gives epochSecond=-62, nanoOfSecond=500_000_000.
  • InstantPatternDynamicFormatter keyed its per-minute cache with epochSecond / 60 and rendered the seconds with epochSecond % 60. Before the epoch the seconds were negative and the wrong cached minute was selected: %d{HH:mm:ss.SSS} printed 23:59:+'.000 for -59_000 ms instead of 23:59:01.000. These now use Math.floorDiv and Math.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: InstantPatternThreadLocalCachedFormatter starts its cache with the epoch instant -1, so the first event on a thread at exactly second (or millisecond) -1 prints 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 that getEpochMillisecond() and getNanoOfMillisecond() round-trip. Four fail on 2.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 with DateTimeFormatter as the existing output_should_match_DateTimeFormatter does. 42 of the 48 cases fail on 2.x; the six that pass are the -60 s cases, which fall on a minute boundary.

Verification

  • The core.time, core.util.internal.instant, core.util.datetime, core.pattern, core.layout and core.impl test packages of log4j-core-test pass in the default Surefire execution (8,675 tests, 0 failures), and spotless is green on all modules. The test-without-jackson-databind execution of this module cannot load the Jackson-based layout tests that my package filter selects, so I read the results per execution.
  • Outside the repository, I compared InstantPatternDynamicFormatter with DateTimeFormatter over 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 example Africa/Monrovia before 1972, which InstantPatternDynamicFormatterTest already excludes) and the -1 case above.
  • I did not run the complete ./mvnw verify of 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.x and pass with the change, and the tests and checks above were run before opening this PR.

Checklist

  • Base your changes on 2.x branch if you are targeting Log4j 2; use main otherwise
  • ./mvnw verify succeeds: not run for the whole reactor locally, see Verification above
  • Non-trivial changes contain an entry file in the src/changelog/.2.x.x directory
  • Tests are provided (MutableInstantTest: 1 new parameterized test, InstantPatternDynamicFormatterTest: 1 new parameterized test)

`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
@ramanathan1504

Copy link
Copy Markdown
Contributor

The floorDiv and floorMod changes match java.time, and the new tests fail on 2.x without them.
The toEpochMinutes change in InstantPatternDynamicFormatter has no test, because each test case builds a new formatter and the cached minute is never reused. Can we add a test that formats 30 s and then -30 s with one formatter?

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 ramanathan1504 added this to the 2.27.0 milestone Oct 21, 2026
@ramanathan1504
ramanathan1504 enabled auto-merge (squash) October 21, 2026 12:34
@sharanggupta

Copy link
Copy Markdown
Contributor Author

Added, thanks — pushed as d5b90a2. Confirmed it catches the regression: reverting toEpochMinutes to the truncating / 60 fails this test (the cached minute is reused, so -30s formats as 00:00:30 instead of 23:59:30); with Math.floorDiv it passes. Full InstantPatternDynamicFormatterTest: 4588 tests, 0 failures.

@ramanathan1504
ramanathan1504 merged commit a38a2f5 into apache:2.x Oct 21, 2026
13 checks passed
@ramanathan1504 ramanathan1504 modified the milestones: 2.27.0, 2.26.2 Nov 8, 2026
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

Instants before the epoch are formatted incorrectly

2 participants