Skip to content

Commit 6d92f2f

Browse files
authored
Fix roundUp parsing with composite patterns backport(#43080) (#43192)
roundUp parsers were losing the composite pattern information when new JavaDateFormatter was created from methods withLocale or withZone. The roundUp parser should be preserved when calling these methods. This is the same approach in withLocale/Zone methods as in https://github.com/elastic/elasticsearch/blob/daa2ec8a605d385a65b9ab3e89d016b3fd0dffe2/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java closes #42835
1 parent 1c843d4 commit 6d92f2f

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Map;
3939
import java.util.Objects;
4040
import java.util.function.Consumer;
41+
import java.util.stream.Collectors;
4142

4243
class JavaDateFormatter implements DateFormatter {
4344

@@ -58,6 +59,12 @@ class JavaDateFormatter implements DateFormatter {
5859
private final List<DateTimeFormatter> parsers;
5960
private final DateTimeFormatter roundupParser;
6061

62+
private JavaDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter roundupParser, List<DateTimeFormatter> parsers) {
63+
this.format = format;
64+
this.printer = printer;
65+
this.roundupParser = roundupParser;
66+
this.parsers = parsers;
67+
}
6168
JavaDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter... parsers) {
6269
this(format, printer, builder -> ROUND_UP_BASE_FIELDS.forEach(builder::parseDefaulting), parsers);
6370
}
@@ -155,9 +162,8 @@ public DateFormatter withZone(ZoneId zoneId) {
155162
if (zoneId.equals(zone())) {
156163
return this;
157164
}
158-
159-
return new JavaDateFormatter(format, printer.withZone(zoneId),
160-
parsers.stream().map(p -> p.withZone(zoneId)).toArray(size -> new DateTimeFormatter[size]));
165+
return new JavaDateFormatter(format, printer.withZone(zoneId), getRoundupParser().withZone(zoneId),
166+
parsers.stream().map(p -> p.withZone(zoneId)).collect(Collectors.toList()));
161167
}
162168

163169
@Override
@@ -166,9 +172,8 @@ public DateFormatter withLocale(Locale locale) {
166172
if (locale.equals(locale())) {
167173
return this;
168174
}
169-
170-
return new JavaDateFormatter(format, printer.withLocale(locale),
171-
parsers.stream().map(p -> p.withLocale(locale)).toArray(size -> new DateTimeFormatter[size]));
175+
return new JavaDateFormatter(format, printer.withLocale(locale), getRoundupParser().withLocale(locale),
176+
parsers.stream().map(p -> p.withLocale(locale)).collect(Collectors.toList()));
172177
}
173178

174179
@Override

server/src/test/java/org/elasticsearch/common/time/JavaDateMathParserTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ public class JavaDateMathParserTests extends ESTestCase {
3737
private final DateFormatter formatter = DateFormatter.forPattern("dateOptionalTime||epoch_millis");
3838
private final DateMathParser parser = formatter.toDateMathParser();
3939

40+
public void testOverridingLocaleOrZoneAndCompositeRoundUpParser() {
41+
//the pattern has to be composite and the match should not be on the first one
42+
DateFormatter formatter = DateFormatter.forPattern("date||epoch_millis").withLocale(randomLocale(random()));
43+
DateMathParser parser = formatter.toDateMathParser();
44+
long gotMillis = parser.parse("297276785531", () -> 0, true, (ZoneId) null).toEpochMilli();
45+
assertDateEquals(gotMillis, "297276785531", "297276785531");
46+
47+
formatter = DateFormatter.forPattern("date||epoch_millis").withZone(randomZone());
48+
parser = formatter.toDateMathParser();
49+
gotMillis = parser.parse("297276785531", () -> 0, true, (ZoneId) null).toEpochMilli();
50+
assertDateEquals(gotMillis, "297276785531", "297276785531");
51+
}
52+
4053
public void testBasicDates() {
4154
assertDateMathEquals("2014-05-30", "2014-05-30T00:00:00.000");
4255
assertDateMathEquals("2014-05-30T20", "2014-05-30T20:00:00.000");

0 commit comments

Comments
 (0)