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 e4065438cbd..332613b13d2 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 @@ void compatibilityOfLegacyPattern(NamedInstantPattern namedPattern) { 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 00000000000..f8b4a86e8a0 --- /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 aaf380c7f0a..9d57493e5d6 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 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 00000000000..e5f9cfa5a07 --- /dev/null +++ b/src/changelog/.2.x.x/3816_legacy_instant_pattern_precision.xml @@ -0,0 +1,8 @@ + + + + Fix precision detection in `InstantPatternLegacyFormatter` for legacy fractional-second patterns using `n` +