This is an automated email from the ASF dual-hosted git repository.

vy pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/2.x by this push:
     new 246fc76eea fix: `InstantPatternLegacyFormatter` precision for legacy 
`n` patterns (#4220)
246fc76eea is described below

commit 246fc76eea147ea067e0924e92bb85580b90a336
Author: Sebastien Tardif <[email protected]>
AuthorDate: Thu Aug 13 03:33:41 2026 -0700

    fix: `InstantPatternLegacyFormatter` precision for legacy `n` patterns 
(#4220)
    
    Signed-off-by: Sebastien Tardif <[email protected]>
    Co-authored-by: Volkan Yazıcı <[email protected]>
---
 .../core/pattern/NamedInstantPatternTest.java      |  1 +
 ...InstantPatternLegacyFormatterPrecisionTest.java | 60 ++++++++++++++++++++++
 .../instant/InstantPatternLegacyFormatter.java     |  5 +-
 .../3816_legacy_instant_pattern_precision.xml      | 11 ++++
 4 files changed, 76 insertions(+), 1 deletion(-)

diff --git 
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/NamedInstantPatternTest.java
 
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/NamedInstantPatternTest.java
index e4065438cb..332613b13d 100644
--- 
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/NamedInstantPatternTest.java
+++ 
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/NamedInstantPatternTest.java
@@ -55,5 +55,6 @@ class NamedInstantPatternTest {
         String legacy = legacyFormatter.format(instant);
         String modern = formatter.format(instant);
         assertThat(legacy).isEqualTo(modern);
+        
assertThat(legacyFormatter.getPrecision()).isEqualTo(formatter.getPrecision());
     }
 }
diff --git 
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/internal/instant/InstantPatternLegacyFormatterPrecisionTest.java
 
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/internal/instant/InstantPatternLegacyFormatterPrecisionTest.java
new file mode 100644
index 0000000000..f8b4a86e8a
--- /dev/null
+++ 
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/internal/instant/InstantPatternLegacyFormatterPrecisionTest.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.log4j.core.util.internal.instant;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.temporal.ChronoUnit;
+import java.util.Locale;
+import java.util.TimeZone;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Regression for issue #3816: legacy fractional-second letter {@code n} must 
not be
+ * treated as always-nanosecond when computing cache precision.
+ */
+class InstantPatternLegacyFormatterPrecisionTest {
+
+    @Test
+    void microsPatternUsingLegacyNUsesMicroPrecision() {
+        final InstantPatternFormatter legacy = 
InstantPatternFormatter.newBuilder()
+                .setLegacyFormattersEnabled(true)
+                .setPattern("yyyy-MM-dd HH:mm:ss,nnnnnn")
+                .setLocale(Locale.US)
+                .setTimeZone(TimeZone.getTimeZone("UTC"))
+                .build();
+        final InstantPatternFormatter modern = 
InstantPatternFormatter.newBuilder()
+                .setLegacyFormattersEnabled(false)
+                .setPattern("yyyy-MM-dd HH:mm:ss,SSSSSS")
+                .setLocale(Locale.US)
+                .setTimeZone(TimeZone.getTimeZone("UTC"))
+                .build();
+        assertThat(legacy.getPrecision()).isEqualTo(ChronoUnit.MICROS);
+        assertThat(legacy.getPrecision()).isEqualTo(modern.getPrecision());
+    }
+
+    @Test
+    void millisPatternUsingLegacySssUsesMilliPrecision() {
+        final InstantPatternFormatter legacy = 
InstantPatternFormatter.newBuilder()
+                .setLegacyFormattersEnabled(true)
+                .setPattern("yyyy-MM-dd HH:mm:ss,SSS")
+                .setLocale(Locale.US)
+                .setTimeZone(TimeZone.getTimeZone("UTC"))
+                .build();
+        assertThat(legacy.getPrecision()).isEqualTo(ChronoUnit.MILLIS);
+    }
+}
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/instant/InstantPatternLegacyFormatter.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/instant/InstantPatternLegacyFormatter.java
index aaf380c7f0..9d57493e5d 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/instant/InstantPatternLegacyFormatter.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/instant/InstantPatternLegacyFormatter.java
@@ -46,7 +46,10 @@ final class InstantPatternLegacyFormatter implements 
InstantPatternFormatter {
     private final BiConsumer<StringBuilder, Instant> formatter;
 
     InstantPatternLegacyFormatter(final String pattern, final Locale locale, 
final TimeZone timeZone) {
-        this.precision = new InstantPatternDynamicFormatter(pattern, locale, 
timeZone).getPrecision();
+        // Replaces 'n' used in legacy patterns with 'S' to obtain the right 
precision.
+        // In legacy patterns, the precision of 'n' depends on the pattern 
length, but
+        // in `DateTimeFormatter`, it is always nanoseconds.
+        this.precision = new 
InstantPatternDynamicFormatter(pattern.replace('n', 'S'), locale, 
timeZone).getPrecision();
         this.pattern = pattern;
         this.locale = locale;
         this.timeZone = timeZone;
diff --git a/src/changelog/.2.x.x/3816_legacy_instant_pattern_precision.xml 
b/src/changelog/.2.x.x/3816_legacy_instant_pattern_precision.xml
new file mode 100644
index 0000000000..6fd2f4c120
--- /dev/null
+++ b/src/changelog/.2.x.x/3816_legacy_instant_pattern_precision.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xmlns="https://logging.apache.org/xml/ns";
+       xsi:schemaLocation="https://logging.apache.org/xml/ns 
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd";
+       type="fixed">
+  <issue id="3816" 
link="https://github.com/apache/logging-log4j2/issues/3816"/>
+  <issue id="4220" link="https://github.com/apache/logging-log4j2/pull/4220"/>
+  <description format="asciidoc">
+    Fix precision detection in legacy date &amp; time pattern formatting
+  </description>
+</entry>

Reply via email to