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 daa2ec8a60/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java
closes #42835
This commit is contained in:
parent
41ac1cd16c
commit
d27c0fd50d
|
@ -38,6 +38,7 @@ import java.util.Locale;
|
|||
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<DateTimeFormatter> parsers;
|
||||
private final DateTimeFormatter roundupParser;
|
||||
|
||||
private JavaDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter roundupParser, List<DateTimeFormatter> 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 @@ class JavaDateFormatter implements DateFormatter {
|
|||
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 @@ class JavaDateFormatter implements DateFormatter {
|
|||
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
|
||||
|
|
|
@ -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");
|
||||
|
|
Loading…
Reference in New Issue