diff --git a/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java b/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java index d0f4200b3bafe..3513600582278 100644 --- a/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java @@ -38,6 +38,7 @@ import java.util.Map; import java.util.Objects; import java.util.function.Consumer; +import java.util.stream.Collectors; class JavaDateFormatter implements DateFormatter { @@ -58,6 +59,12 @@ class JavaDateFormatter implements DateFormatter { private final List parsers; private final DateTimeFormatter roundupParser; + private JavaDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter roundupParser, List parsers) { + this.format = format; + this.printer = printer; + this.roundupParser = roundupParser; + this.parsers = parsers; + } JavaDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter... parsers) { this(format, printer, builder -> ROUND_UP_BASE_FIELDS.forEach(builder::parseDefaulting), parsers); } @@ -155,9 +162,8 @@ public DateFormatter withZone(ZoneId zoneId) { if (zoneId.equals(zone())) { return this; } - - return new JavaDateFormatter(format, printer.withZone(zoneId), - parsers.stream().map(p -> p.withZone(zoneId)).toArray(size -> new DateTimeFormatter[size])); + return new JavaDateFormatter(format, printer.withZone(zoneId), getRoundupParser().withZone(zoneId), + parsers.stream().map(p -> p.withZone(zoneId)).collect(Collectors.toList())); } @Override @@ -166,9 +172,8 @@ public DateFormatter withLocale(Locale locale) { if (locale.equals(locale())) { return this; } - - return new JavaDateFormatter(format, printer.withLocale(locale), - parsers.stream().map(p -> p.withLocale(locale)).toArray(size -> new DateTimeFormatter[size])); + return new JavaDateFormatter(format, printer.withLocale(locale), getRoundupParser().withLocale(locale), + parsers.stream().map(p -> p.withLocale(locale)).collect(Collectors.toList())); } @Override diff --git a/server/src/test/java/org/elasticsearch/common/time/JavaDateMathParserTests.java b/server/src/test/java/org/elasticsearch/common/time/JavaDateMathParserTests.java index 7fc7a83523c66..2e30b33ab96cf 100644 --- a/server/src/test/java/org/elasticsearch/common/time/JavaDateMathParserTests.java +++ b/server/src/test/java/org/elasticsearch/common/time/JavaDateMathParserTests.java @@ -37,6 +37,19 @@ public class JavaDateMathParserTests extends ESTestCase { private final DateFormatter formatter = DateFormatter.forPattern("dateOptionalTime||epoch_millis"); private final DateMathParser parser = formatter.toDateMathParser(); + public void testOverridingLocaleOrZoneAndCompositeRoundUpParser() { + //the pattern has to be composite and the match should not be on the first one + DateFormatter formatter = DateFormatter.forPattern("date||epoch_millis").withLocale(randomLocale(random())); + DateMathParser parser = formatter.toDateMathParser(); + long gotMillis = parser.parse("297276785531", () -> 0, true, (ZoneId) null).toEpochMilli(); + assertDateEquals(gotMillis, "297276785531", "297276785531"); + + formatter = DateFormatter.forPattern("date||epoch_millis").withZone(randomZone()); + parser = formatter.toDateMathParser(); + gotMillis = parser.parse("297276785531", () -> 0, true, (ZoneId) null).toEpochMilli(); + assertDateEquals(gotMillis, "297276785531", "297276785531"); + } + public void testBasicDates() { assertDateMathEquals("2014-05-30", "2014-05-30T00:00:00.000"); assertDateMathEquals("2014-05-30T20", "2014-05-30T20:00:00.000");