Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?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"/>
<description format="asciidoc">Fix precision detection in `InstantPatternLegacyFormatter` for legacy fractional-second patterns using `n`</description>
</entry>