From d31a3e8f220806004eee43ebc4427c49aef3b0f1 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Wed, 11 Sep 2019 17:24:33 +0200 Subject: [PATCH 01/12] refactor round up builder --- .../common/time/DateFormatter.java | 2 +- .../common/time/DateFormatters.java | 109 ++++++++---------- .../elasticsearch/common/time/EpochTime.java | 4 +- .../common/time/JavaDateFormatter.java | 103 +++++++++++++---- .../common/time/JavaDateMathParser.java | 9 +- .../common/joda/JodaDateMathParserTests.java | 14 +++ .../common/time/DateFormattersTests.java | 15 ++- .../common/time/JavaDateMathParserTests.java | 7 +- 8 files changed, 162 insertions(+), 101 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java b/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java index bf7999067b05a..367aecb74996b 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java @@ -149,6 +149,6 @@ static DateFormatter forPattern(String input) { return formatters.get(0); } - return DateFormatters.merge(input, formatters); + return new JavaDateFormatter(input, formatters); } } diff --git a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java index 8cb71866ad252..f0fcc868fe2e2 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java @@ -40,8 +40,6 @@ import java.time.temporal.TemporalAdjusters; import java.time.temporal.TemporalQueries; import java.time.temporal.WeekFields; -import java.util.ArrayList; -import java.util.List; import static java.time.temporal.ChronoField.DAY_OF_MONTH; import static java.time.temporal.ChronoField.DAY_OF_WEEK; @@ -324,7 +322,7 @@ public class DateFormatters { /* * Returns a basic formatter that combines a basic date and time, separated - * by a 'T' (yyyyMMdd'T'HHmmss.SSSZ). + * by a 'T' (uuuuMMdd'T'HHmmss.SSSZ). */ private static final DateFormatter BASIC_DATE_TIME = new JavaDateFormatter("basic_date_time", new DateTimeFormatterBuilder().append(BASIC_DATE_TIME_PRINTER).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), @@ -337,7 +335,7 @@ public class DateFormatters { /* * Returns a basic formatter that combines a basic date and time without millis, - * separated by a 'T' (yyyyMMdd'T'HHmmssZ). + * separated by a 'T' (uuuuMMdd'T'HHmmssZ). */ private static final DateFormatter BASIC_DATE_TIME_NO_MILLIS = new JavaDateFormatter("basic_date_time_no_millis", new DateTimeFormatterBuilder().append(BASIC_DATE_T).append(BASIC_TIME_NO_MILLIS_BASE) @@ -350,35 +348,39 @@ public class DateFormatters { /* * Returns a formatter for a full ordinal date, using a four - * digit year and three digit dayOfYear (yyyyDDD). + * digit year and three digit dayOfYear (uuuuDDD). */ private static final DateFormatter BASIC_ORDINAL_DATE = new JavaDateFormatter("basic_ordinal_date", - DateTimeFormatter.ofPattern("yyyyDDD", IsoLocale.ROOT)); + DateTimeFormatter.ofPattern("uuuuDDD", IsoLocale.ROOT)); /* * Returns a formatter for a full ordinal date and time, using a four - * digit year and three digit dayOfYear (yyyyDDD'T'HHmmss.SSSZ). + * digit year and three digit dayOfYear (uuuuDDD'T'HHmmss.SSSZ). */ private static final DateFormatter BASIC_ORDINAL_DATE_TIME = new JavaDateFormatter("basic_ordinal_date_time", - new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_PRINTER) - .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), - new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_FORMATTER) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), - new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_FORMATTER) + new DateTimeFormatterBuilder().appendPattern("uuuuDDD").append(BASIC_T_TIME_PRINTER) + .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), + new DateTimeFormatterBuilder().appendPattern("uuuuDDD").append(BASIC_T_TIME_FORMATTER) + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), + new DateTimeFormatterBuilder().appendPattern("uuuuDDD").append(BASIC_T_TIME_FORMATTER) .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) ); /* * Returns a formatter for a full ordinal date and time without millis, - * using a four digit year and three digit dayOfYear (yyyyDDD'T'HHmmssZ). + * using a four digit year and three digit dayOfYear (uuuuDDD'T'HHmmssZ). */ private static final DateFormatter BASIC_ORDINAL_DATE_TIME_NO_MILLIS = new JavaDateFormatter("basic_ordinal_date_time_no_millis", - new DateTimeFormatterBuilder().appendPattern("yyyyDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) - .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), - new DateTimeFormatterBuilder().appendPattern("yyyyDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), - new DateTimeFormatterBuilder().appendPattern("yyyyDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) + new DateTimeFormatterBuilder().appendPattern("uuuuDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) + .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), + new DateTimeFormatterBuilder().appendPattern("uuuuDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), + new DateTimeFormatterBuilder().appendPattern("uuuuDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) ); @@ -494,13 +496,13 @@ public class DateFormatters { * A date formatter that formats or parses a date plus an hour without an offset, such as '2011-12-03T01'. */ private static final DateFormatter STRICT_DATE_HOUR = new JavaDateFormatter("strict_date_hour", - DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH", IsoLocale.ROOT)); + DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH", IsoLocale.ROOT)); /* * A date formatter that formats or parses a date plus an hour/minute without an offset, such as '2011-12-03T01:10'. */ private static final DateFormatter STRICT_DATE_HOUR_MINUTE = new JavaDateFormatter("strict_date_hour_minute", - DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm", IsoLocale.ROOT)); + DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm", IsoLocale.ROOT)); /* * A strict date formatter that formats or parses a date without an offset, such as '2011-12-03'. @@ -550,7 +552,7 @@ public class DateFormatters { /* * Returns a formatter that combines a full date and time, separated by a 'T' - * (yyyy-MM-dd'T'HH:mm:ss.SSSZZ). + * (uuuu-MM-dd'T'HH:mm:ss.SSSZZ). */ private static final DateFormatter STRICT_DATE_TIME = new JavaDateFormatter("strict_date_time", STRICT_DATE_PRINTER, new DateTimeFormatterBuilder().append(STRICT_DATE_FORMATTER).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), @@ -568,7 +570,7 @@ public class DateFormatters { /* * Returns a formatter for a full ordinal date and time without millis, - * using a four digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ssZZ). + * using a four digit year and three digit dayOfYear (uuuu-DDD'T'HH:mm:ssZZ). */ private static final DateFormatter STRICT_ORDINAL_DATE_TIME_NO_MILLIS = new JavaDateFormatter("strict_ordinal_date_time_no_millis", new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_NO_MILLIS_BASE) @@ -587,7 +589,7 @@ public class DateFormatters { /* * Returns a formatter that combines a full date and time without millis, - * separated by a 'T' (yyyy-MM-dd'T'HH:mm:ssZZ). + * separated by a 'T' (uuuu-MM-dd'T'HH:mm:ssZZ). */ private static final DateFormatter STRICT_DATE_TIME_NO_MILLIS = new JavaDateFormatter("strict_date_time_no_millis", new DateTimeFormatterBuilder().append(STRICT_DATE_TIME_NO_MILLIS_FORMATTER) @@ -628,7 +630,7 @@ public class DateFormatters { /* * Returns a formatter that combines a full date, two digit hour of day, * two digit minute of hour, two digit second of minute, and three digit - * fraction of second (yyyy-MM-dd'T'HH:mm:ss.SSS). + * fraction of second (uuuu-MM-dd'T'HH:mm:ss.SSS). */ private static final DateFormatter STRICT_DATE_HOUR_MINUTE_SECOND_FRACTION = new JavaDateFormatter( "strict_date_hour_minute_second_fraction", @@ -703,7 +705,7 @@ public class DateFormatters { /* * Returns a formatter for a full ordinal date and time, using a four - * digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ss.SSSZZ). + * digit year and three digit dayOfYear (uuuu-DDD'T'HH:mm:ss.SSSZZ). */ private static final DateFormatter STRICT_ORDINAL_DATE_TIME = new JavaDateFormatter("strict_ordinal_date_time", new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_PRINTER) @@ -874,14 +876,14 @@ public class DateFormatters { /* * Returns a formatter that combines a full date, two digit hour of day, * two digit minute of hour, and two digit second of - * minute. (yyyy-MM-dd'T'HH:mm:ss) + * minute. (uuuu-MM-dd'T'HH:mm:ss) */ private static final DateFormatter STRICT_DATE_HOUR_MINUTE_SECOND = new JavaDateFormatter("strict_date_hour_minute_second", - DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", IsoLocale.ROOT)); + DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss", IsoLocale.ROOT)); /* * A basic formatter for a full date as four digit year, two digit - * month of year, and two digit day of month (yyyyMMdd). + * month of year, and two digit day of month (uuuuMMdd). */ private static final DateFormatter BASIC_DATE = new JavaDateFormatter("basic_date", new DateTimeFormatterBuilder() @@ -906,7 +908,7 @@ public class DateFormatters { /* * Returns a formatter for a full ordinal date, using a four - * digit year and three digit dayOfYear (yyyy-DDD). + * digit year and three digit dayOfYear (uuuu-DDD). */ private static final DateFormatter STRICT_ORDINAL_DATE = new JavaDateFormatter("strict_ordinal_date", STRICT_ORDINAL_DATE_FORMATTER); @@ -940,7 +942,7 @@ public class DateFormatters { /* * a date formatter with optional time, being very lenient, format is - * yyyy-MM-dd'T'HH:mm:ss.SSSZ + * uuuu-MM-dd'T'HH:mm:ss.SSSZ */ private static final DateFormatter DATE_OPTIONAL_TIME = new JavaDateFormatter("date_optional_time", STRICT_DATE_OPTIONAL_TIME_PRINTER, @@ -1009,7 +1011,7 @@ public class DateFormatters { /* * Returns a formatter for a full ordinal date, using a four - * digit year and three digit dayOfYear (yyyy-DDD). + * digit year and three digit dayOfYear (uuuu-DDD). */ private static final DateFormatter ORDINAL_DATE = new JavaDateFormatter("ordinal_date", ORDINAL_DATE_PRINTER, ORDINAL_DATE_FORMATTER); @@ -1052,10 +1054,10 @@ public class DateFormatters { /* * Returns a formatter that combines a full date and two digit hour of - * day. (yyyy-MM-dd'T'HH) + * day. (uuuu-MM-dd'T'HH) */ private static final DateFormatter DATE_HOUR = new JavaDateFormatter("date_hour", - DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH", IsoLocale.ROOT), + DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH", IsoLocale.ROOT), new DateTimeFormatterBuilder() .append(DATE_FORMATTER) .appendLiteral("T") @@ -1065,7 +1067,7 @@ public class DateFormatters { /* * Returns a formatter that combines a full date, two digit hour of day, * two digit minute of hour, two digit second of minute, and three digit - * fraction of second (yyyy-MM-dd'T'HH:mm:ss.SSS). + * fraction of second (uuuu-MM-dd'T'HH:mm:ss.SSS). */ private static final DateFormatter DATE_HOUR_MINUTE_SECOND_MILLIS = new JavaDateFormatter("date_hour_minute_second_millis", @@ -1095,10 +1097,10 @@ public class DateFormatters { /* * Returns a formatter that combines a full date, two digit hour of day, - * and two digit minute of hour. (yyyy-MM-dd'T'HH:mm) + * and two digit minute of hour. (uuuu-MM-dd'T'HH:mm) */ private static final DateFormatter DATE_HOUR_MINUTE = new JavaDateFormatter("date_hour_minute", - DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm", IsoLocale.ROOT), + DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm", IsoLocale.ROOT), new DateTimeFormatterBuilder() .append(DATE_FORMATTER) .appendLiteral("T") @@ -1108,10 +1110,10 @@ public class DateFormatters { /* * Returns a formatter that combines a full date, two digit hour of day, * two digit minute of hour, and two digit second of - * minute. (yyyy-MM-dd'T'HH:mm:ss) + * minute. (uuuu-MM-dd'T'HH:mm:ss) */ private static final DateFormatter DATE_HOUR_MINUTE_SECOND = new JavaDateFormatter("date_hour_minute_second", - DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", IsoLocale.ROOT), + DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss", IsoLocale.ROOT), new DateTimeFormatterBuilder() .append(DATE_FORMATTER) .appendLiteral("T") @@ -1131,7 +1133,7 @@ public class DateFormatters { /* * Returns a formatter that combines a full date and time, separated by a 'T' - * (yyyy-MM-dd'T'HH:mm:ss.SSSZZ). + * (uuuu-MM-dd'T'HH:mm:ss.SSSZZ). */ private static final DateFormatter DATE_TIME = new JavaDateFormatter("date_time", STRICT_DATE_OPTIONAL_TIME_PRINTER, @@ -1150,7 +1152,7 @@ public class DateFormatters { /* * Returns a formatter for a full date as four digit year, two digit month - * of year, and two digit day of month (yyyy-MM-dd). + * of year, and two digit day of month (uuuu-MM-dd). */ private static final DateFormatter DATE = new JavaDateFormatter("date", DateTimeFormatter.ISO_LOCAL_DATE.withResolverStyle(ResolverStyle.LENIENT), @@ -1178,7 +1180,7 @@ public class DateFormatters { /* * Returns a formatter that combines a full date and time without millis, but with a timezone that can be optional - * separated by a 'T' (yyyy-MM-dd'T'HH:mm:ssZ). + * separated by a 'T' (uuuu-MM-dd'T'HH:mm:ssZ). */ private static final DateFormatter DATE_TIME_NO_MILLIS = new JavaDateFormatter("date_time_no_millis", DATE_TIME_NO_MILLIS_PRINTER, @@ -1242,7 +1244,7 @@ public class DateFormatters { /* * Returns a formatter for a full ordinal date and time, using a four - * digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ss.SSSZZ). + * digit year and three digit dayOfYear (uuuu-DDD'T'HH:mm:ss.SSSZZ). */ private static final DateFormatter ORDINAL_DATE_TIME = new JavaDateFormatter("ordinal_date_time", new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_PRINTER) @@ -1261,7 +1263,7 @@ public class DateFormatters { /* * Returns a formatter for a full ordinal date and time without millis, - * using a four digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ssZZ). + * using a four digit year and three digit dayOfYear (uuuu-DDD'T'HH:mm:ssZZ). */ private static final DateFormatter ORDINAL_DATE_TIME_NO_MILLIS = new JavaDateFormatter("ordinal_date_time_no_millis", new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_NO_MILLIS_BASE) @@ -1440,7 +1442,7 @@ public class DateFormatters { .appendValue(WeekFields.ISO.dayOfWeek()) .toFormatter(IsoLocale.ROOT) ); - + ///////////////////////////////////////// // @@ -1628,26 +1630,7 @@ static DateFormatter forPattern(String input) { } } - static JavaDateFormatter merge(String pattern, List formatters) { - assert formatters.size() > 0; - - List dateTimeFormatters = new ArrayList<>(formatters.size()); - DateTimeFormatterBuilder roundupBuilder = new DateTimeFormatterBuilder(); - DateTimeFormatter printer = null; - for (DateFormatter formatter : formatters) { - assert formatter instanceof JavaDateFormatter; - JavaDateFormatter javaDateFormatter = (JavaDateFormatter) formatter; - if (printer == null) { - printer = javaDateFormatter.getPrinter(); - } - dateTimeFormatters.addAll(javaDateFormatter.getParsers()); - roundupBuilder.appendOptional(javaDateFormatter.getRoundupParser()); - } - DateTimeFormatter roundUpParser = roundupBuilder.toFormatter(IsoLocale.ROOT); - return new JavaDateFormatter(pattern, printer, builder -> builder.append(roundUpParser), - dateTimeFormatters.toArray(new DateTimeFormatter[0])); - } private static final LocalDate LOCALDATE_EPOCH = LocalDate.of(1970, 1, 1); diff --git a/server/src/main/java/org/elasticsearch/common/time/EpochTime.java b/server/src/main/java/org/elasticsearch/common/time/EpochTime.java index 5875e8572aebb..e718ef40040ad 100644 --- a/server/src/main/java/org/elasticsearch/common/time/EpochTime.java +++ b/server/src/main/java/org/elasticsearch/common/time/EpochTime.java @@ -148,11 +148,11 @@ public long getFrom(TemporalAccessor temporal) { .toFormatter(IsoLocale.ROOT); static final DateFormatter SECONDS_FORMATTER = new JavaDateFormatter("epoch_second", SECONDS_FORMATTER1, - builder -> builder.parseDefaulting(ChronoField.NANO_OF_SECOND, 999_999_999L), + builder -> builder.parseDefaulting(ChronoField.NANO_OF_SECOND, 999_999_999L),null, SECONDS_FORMATTER1, SECONDS_FORMATTER2); static final DateFormatter MILLIS_FORMATTER = new JavaDateFormatter("epoch_millis", MILLISECONDS_FORMATTER1, - builder -> builder.parseDefaulting(EpochTime.NANOS_OF_MILLI, 999_999L), + builder -> builder.parseDefaulting(EpochTime.NANOS_OF_MILLI, 999_999L),null, MILLISECONDS_FORMATTER1, MILLISECONDS_FORMATTER2); private abstract static class EpochField implements TemporalField { 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 3513600582278..5ae6ec5a569b1 100644 --- a/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java @@ -29,6 +29,7 @@ import java.time.temporal.ChronoField; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalField; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -57,19 +58,67 @@ class JavaDateFormatter implements DateFormatter { private final String format; private final DateTimeFormatter printer; private final List parsers; - private final DateTimeFormatter roundupParser; + private final JavaDateFormatter roundupParser; - private JavaDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter roundupParser, List parsers) { - this.format = format; - this.printer = printer; - this.roundupParser = roundupParser; - this.parsers = parsers; + static class RoundUpFormatter extends JavaDateFormatter{ + private final List roundUpParsers; + + RoundUpFormatter(String format, List roundUpParsers) { + super(format, null,null, roundUpParsers); + this.roundUpParsers = roundUpParsers; + } + + @Override + public ZoneId zone() { + return roundUpParsers.get(0).getZone(); + } + + @Override + public Locale locale() { + return roundUpParsers.get(0).getLocale(); + } } + + // named formatters JavaDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter... parsers) { - this(format, printer, builder -> ROUND_UP_BASE_FIELDS.forEach(builder::parseDefaulting), parsers); + this(format, printer, builder -> ROUND_UP_BASE_FIELDS.forEach(builder::parseDefaulting), null, parsers); } + //merging + JavaDateFormatter(String pattern, List formatters) { + assert formatters.size() > 0; + + List dateTimeFormatters = new ArrayList<>(formatters.size()); + List roundUpFormatters = new ArrayList<>(formatters.size()); - JavaDateFormatter(String format, DateTimeFormatter printer, Consumer roundupParserConsumer, + DateTimeFormatter printer = null; + for (DateFormatter formatter : formatters) { + assert formatter instanceof JavaDateFormatter; + JavaDateFormatter javaDateFormatter = (JavaDateFormatter) formatter; + if (printer == null) { + printer = javaDateFormatter.getPrinter(); + } + dateTimeFormatters.addAll(javaDateFormatter.getParsers()); + roundUpFormatters.addAll(javaDateFormatter.getRoundupParser().getParsers()); + } + + this.format = pattern; + this.printer = printer; + this.roundupParser = roundUpFormatters!=null ? new RoundUpFormatter(format, roundUpFormatters ) : null; + this.parsers = dateTimeFormatters; + } + + // zone & locale and roundup + JavaDateFormatter(String format, DateTimeFormatter printer, List roundUpParsers, List parsers) { + this.format = format; + this.printer = printer; + this.roundupParser = roundUpParsers!=null ? new RoundUpFormatter(format, roundUpParsers ) : null; + this.parsers = parsers; + } + // subclasses + JavaDateFormatter(String format, + DateTimeFormatter printer, + Consumer roundupParserConsumer, + List roundUpParsers, DateTimeFormatter... parsers) { if (printer == null) { throw new IllegalArgumentException("printer may not be null"); @@ -91,19 +140,23 @@ private JavaDateFormatter(String format, DateTimeFormatter printer, DateTimeForm this.parsers = Arrays.asList(parsers); } - DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder(); + List roundUp = createRoundUpParsers(format,roundupParserConsumer, roundUpParsers); + this.roundupParser = new RoundUpFormatter(format, roundUp) ; + } + + private List createRoundUpParsers(String format, + Consumer roundupParserConsumer, + List roundUpParsers) { if (format.contains("||") == false) { + DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder(); builder.append(this.parsers.get(0)); + roundupParserConsumer.accept(builder); + return Arrays.asList(builder.toFormatter(locale())); } - roundupParserConsumer.accept(builder); - DateTimeFormatter roundupFormatter = builder.toFormatter(locale()); - if (printer.getZone() != null) { - roundupFormatter = roundupFormatter.withZone(zone()); - } - this.roundupParser = roundupFormatter; + return roundUpParsers; } - DateTimeFormatter getRoundupParser() { + JavaDateFormatter getRoundupParser() { return roundupParser; } @@ -162,8 +215,12 @@ public DateFormatter withZone(ZoneId zoneId) { if (zoneId.equals(zone())) { return this; } - return new JavaDateFormatter(format, printer.withZone(zoneId), getRoundupParser().withZone(zoneId), - parsers.stream().map(p -> p.withZone(zoneId)).collect(Collectors.toList())); + List parsers = this.parsers.stream().map(p -> p.withZone(zoneId)).collect(Collectors.toList()); + List roundUpParsers = this.roundupParser.getParsers() + .stream() + .map(p -> p.withZone(zoneId)) + .collect(Collectors.toList()); + return new JavaDateFormatter(format, printer.withZone(zoneId), roundUpParsers, parsers); } @Override @@ -172,8 +229,12 @@ public DateFormatter withLocale(Locale locale) { if (locale.equals(locale())) { return this; } - return new JavaDateFormatter(format, printer.withLocale(locale), getRoundupParser().withLocale(locale), - parsers.stream().map(p -> p.withLocale(locale)).collect(Collectors.toList())); + List parsers = this.parsers.stream().map(p -> p.withLocale(locale)).collect(Collectors.toList()); + List roundUpParsers = this.roundupParser.getParsers() + .stream() + .map(p -> p.withLocale(locale)) + .collect(Collectors.toList()); + return new JavaDateFormatter(format, printer.withLocale(locale), roundUpParsers, parsers); } @Override @@ -198,7 +259,7 @@ public ZoneId zone() { @Override public DateMathParser toDateMathParser() { - return new JavaDateMathParser(format, this, getRoundupParser()); + return new JavaDateMathParser(format, this, roundupParser); } @Override diff --git a/server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java b/server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java index 78d4f10d87cbf..5ab857664046a 100644 --- a/server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java +++ b/server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java @@ -28,7 +28,6 @@ import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.time.temporal.ChronoField; import java.time.temporal.TemporalAccessor; @@ -48,14 +47,14 @@ public class JavaDateMathParser implements DateMathParser { private final JavaDateFormatter formatter; - private final DateTimeFormatter roundUpFormatter; private final String format; + private JavaDateFormatter roundupParser2; - JavaDateMathParser(String format, JavaDateFormatter formatter, DateTimeFormatter roundUpFormatter) { + JavaDateMathParser(String format, JavaDateFormatter formatter, JavaDateFormatter roundupParser2) { this.format = format; + this.roundupParser2 = roundupParser2; Objects.requireNonNull(formatter); this.formatter = formatter; - this.roundUpFormatter = roundUpFormatter; } @Override @@ -217,7 +216,7 @@ private Instant parseDateTime(String value, ZoneId timeZone, boolean roundUpIfNo throw new ElasticsearchParseException("cannot parse empty date"); } - Function formatter = roundUpIfNoTime ? this.roundUpFormatter::parse : this.formatter::parse; + Function formatter = roundUpIfNoTime ? this.roundupParser2::parse : this.formatter::parse; try { if (timeZone == null) { return DateFormatters.from(formatter.apply(value)).toInstant(); diff --git a/server/src/test/java/org/elasticsearch/common/joda/JodaDateMathParserTests.java b/server/src/test/java/org/elasticsearch/common/joda/JodaDateMathParserTests.java index f6382b92343ec..d1a90328dfb65 100644 --- a/server/src/test/java/org/elasticsearch/common/joda/JodaDateMathParserTests.java +++ b/server/src/test/java/org/elasticsearch/common/joda/JodaDateMathParserTests.java @@ -27,6 +27,7 @@ import java.time.Instant; import java.time.ZoneId; +import java.time.ZoneOffset; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.LongSupplier; @@ -59,6 +60,19 @@ void assertDateEquals(long gotMillis, String original, String expected) { } } + public void testOverridingLocaleOrZoneAndCompositeRoundUpParser() { + //the pattern has to be composite and the match should not be on the first one + DateFormatter formatter = Joda.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 = Joda.forPattern("date||epoch_millis").withZone(ZoneOffset.UTC); + parser = formatter.toDateMathParser(); + gotMillis = parser.parse("297276785531", () -> 0, true, (ZoneId) null).toEpochMilli(); + assertDateEquals(gotMillis, "297276785531", "297276785531"); + } + public void testBasicDates() { assertDateMathEquals("2014", "2014-01-01T00:00:00.000"); assertDateMathEquals("2014-05", "2014-05-01T00:00:00.000"); diff --git a/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java b/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java index 1f18fb1655d44..e478b6dfcc499 100644 --- a/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java +++ b/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java @@ -26,7 +26,6 @@ import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoField; import java.time.temporal.TemporalAccessor; import java.util.Locale; @@ -253,7 +252,7 @@ public void testIso8601Parsing() { public void testRoundupFormatterWithEpochDates() { assertRoundupFormatter("epoch_millis", "1234567890", 1234567890L); // also check nanos of the epoch_millis formatter if it is rounded up to the nano second - DateTimeFormatter roundUpFormatter = ((JavaDateFormatter) DateFormatter.forPattern("8epoch_millis")).getRoundupParser(); + JavaDateFormatter roundUpFormatter = ((JavaDateFormatter) DateFormatter.forPattern("8epoch_millis")).getRoundupParser(); Instant epochMilliInstant = DateFormatters.from(roundUpFormatter.parse("1234567890")).toInstant(); assertThat(epochMilliInstant.getLong(ChronoField.NANO_OF_SECOND), is(890_999_999L)); @@ -266,7 +265,7 @@ public void testRoundupFormatterWithEpochDates() { assertRoundupFormatter("epoch_second", "1234567890", 1234567890999L); // also check nanos of the epoch_millis formatter if it is rounded up to the nano second - DateTimeFormatter epochSecondRoundupParser = ((JavaDateFormatter) DateFormatter.forPattern("8epoch_second")).getRoundupParser(); + JavaDateFormatter epochSecondRoundupParser = ((JavaDateFormatter) DateFormatter.forPattern("8epoch_second")).getRoundupParser(); Instant epochSecondInstant = DateFormatters.from(epochSecondRoundupParser.parse("1234567890")).toInstant(); assertThat(epochSecondInstant.getLong(ChronoField.NANO_OF_SECOND), is(999_999_999L)); @@ -280,7 +279,7 @@ public void testRoundupFormatterWithEpochDates() { private void assertRoundupFormatter(String format, String input, long expectedMilliSeconds) { JavaDateFormatter dateFormatter = (JavaDateFormatter) DateFormatter.forPattern(format); dateFormatter.parse(input); - DateTimeFormatter roundUpFormatter = dateFormatter.getRoundupParser(); + JavaDateFormatter roundUpFormatter = dateFormatter.getRoundupParser(); long millis = DateFormatters.from(roundUpFormatter.parse(input)).toInstant().toEpochMilli(); assertThat(millis, is(expectedMilliSeconds)); } @@ -290,8 +289,8 @@ public void testRoundupFormatterZone() { String format = randomFrom("epoch_second", "epoch_millis", "strict_date_optional_time", "uuuu-MM-dd'T'HH:mm:ss.SSS", "strict_date_optional_time||date_optional_time"); JavaDateFormatter formatter = (JavaDateFormatter) DateFormatter.forPattern(format).withZone(zoneId); - DateTimeFormatter roundUpFormatter = formatter.getRoundupParser(); - assertThat(roundUpFormatter.getZone(), is(zoneId)); + JavaDateFormatter roundUpFormatter = formatter.getRoundupParser(); + assertThat(roundUpFormatter.zone(), is(zoneId)); assertThat(formatter.zone(), is(zoneId)); } @@ -300,8 +299,8 @@ public void testRoundupFormatterLocale() { String format = randomFrom("epoch_second", "epoch_millis", "strict_date_optional_time", "uuuu-MM-dd'T'HH:mm:ss.SSS", "strict_date_optional_time||date_optional_time"); JavaDateFormatter formatter = (JavaDateFormatter) DateFormatter.forPattern(format).withLocale(locale); - DateTimeFormatter roundupParser = formatter.getRoundupParser(); - assertThat(roundupParser.getLocale(), is(locale)); + JavaDateFormatter roundupParser = formatter.getRoundupParser(); + assertThat(roundupParser.locale(), is(locale)); assertThat(formatter.locale(), is(locale)); } 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 2fb524608968d..c50ec185dc5d5 100644 --- a/server/src/test/java/org/elasticsearch/common/time/JavaDateMathParserTests.java +++ b/server/src/test/java/org/elasticsearch/common/time/JavaDateMathParserTests.java @@ -44,7 +44,7 @@ public void testOverridingLocaleOrZoneAndCompositeRoundUpParser() { long gotMillis = parser.parse("297276785531", () -> 0, true, (ZoneId) null).toEpochMilli(); assertDateEquals(gotMillis, "297276785531", "297276785531"); - formatter = DateFormatter.forPattern("date||epoch_millis").withZone(randomZone()); + formatter = DateFormatter.forPattern("date||epoch_millis").withZone(ZoneOffset.UTC); parser = formatter.toDateMathParser(); gotMillis = parser.parse("297276785531", () -> 0, true, (ZoneId) null).toEpochMilli(); assertDateEquals(gotMillis, "297276785531", "297276785531"); @@ -301,6 +301,11 @@ private void assertDateMathEquals(String toTest, String expected) { } private void assertDateMathEquals(String toTest, String expected, final long now, boolean roundUp, ZoneId timeZone) { + assertDateMathEquals(parser, toTest, expected, now, roundUp, timeZone); + } + + private void assertDateMathEquals(DateMathParser parser, String toTest, String expected, final long now, + boolean roundUp, ZoneId timeZone) { long gotMillis = parser.parse(toTest, () -> now, roundUp, timeZone).toEpochMilli(); assertDateEquals(gotMillis, toTest, expected); } From e896ed7372badbd891546292b0bff32e7fd4618e Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Thu, 12 Sep 2019 10:07:42 +0200 Subject: [PATCH 02/12] testcase --- .../joda/JavaJodaTimeDuellingTests.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java index 99121fdc835d3..4d73d94610b32 100644 --- a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java +++ b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java @@ -22,12 +22,14 @@ import org.elasticsearch.bootstrap.JavaVersion; import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; +import org.elasticsearch.common.time.DateMathParser; import org.elasticsearch.test.ESTestCase; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.format.ISODateTimeFormat; import java.time.LocalDateTime; +import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; @@ -44,6 +46,26 @@ protected boolean enableWarningsCheck() { return false; } + public void testCompositeParsingDateMath(){ + //in all these examples the second pattern will be used + + DateFormatter javaFormatter = DateFormatter.forPattern("yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSS").withLocale(randomLocale(random())); + DateMathParser javaDateMath = javaFormatter.toDateMathParser(); + long gotMillisJava = javaDateMath.parse("2014-06-06T12:01:02.123", () -> 0, true, (ZoneId) null).toEpochMilli(); + + DateFormatter jodaFormatter = Joda.forPattern("yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSS").withLocale(randomLocale(random())); + DateMathParser jodaDateMath = jodaFormatter.toDateMathParser(); + long gotMillisJoda = jodaDateMath.parse("2014-06-06T12:01:02.123", () -> 0, true, (ZoneId) null).toEpochMilli(); + + assertEquals(gotMillisJoda, gotMillisJava); + +// assertSameDate("2014-06-06T12:01:02.123", "yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSS"); +// assertSameDate("2014-06-06T12:01:02.123", "strictDateTimeNoMillis||yyyy-MM-dd'T'HH:mm:ss.SSS"); +// assertSameDate("2014-06-06T12:01:02.123", "yyyy-MM-dd'T'HH:mm:ss+HH:MM||yyyy-MM-dd'T'HH:mm:ss.SSS"); + } + +// publi + public void testDayOfWeek() { //7 (ok joda) vs 1 (java by default) but 7 with customized org.elasticsearch.common.time.IsoLocale.ISO8601 ZonedDateTime now = LocalDateTime.of(2009,11,15,1,32,8,328402) From 038188efe9518cadbce101964d5e8a963bb8c00f Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Thu, 12 Sep 2019 13:07:51 +0200 Subject: [PATCH 03/12] assertions --- .../joda/JavaJodaTimeDuellingTests.java | 45 ++++++++++++------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java index 4d73d94610b32..c1baabef48b8f 100644 --- a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java +++ b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.common.joda; +import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.bootstrap.JavaVersion; import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; @@ -46,25 +47,32 @@ protected boolean enableWarningsCheck() { return false; } - public void testCompositeParsingDateMath(){ + public void testCompositeDateMathParsing(){ //in all these examples the second pattern will be used + assertDateMathEquals("2014-06-06T12:01:02.123", "yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSS"); + assertDateMathEquals("2014-06-06T12:01:02.123", "strictDateTimeNoMillis||yyyy-MM-dd'T'HH:mm:ss.SSS"); + assertDateMathEquals("2014-06-06T12:01:02.123", "yyyy-MM-dd'T'HH:mm:ss+HH:MM||yyyy-MM-dd'T'HH:mm:ss.SSS"); + } - DateFormatter javaFormatter = DateFormatter.forPattern("yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSS").withLocale(randomLocale(random())); - DateMathParser javaDateMath = javaFormatter.toDateMathParser(); - long gotMillisJava = javaDateMath.parse("2014-06-06T12:01:02.123", () -> 0, true, (ZoneId) null).toEpochMilli(); - - DateFormatter jodaFormatter = Joda.forPattern("yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSS").withLocale(randomLocale(random())); - DateMathParser jodaDateMath = jodaFormatter.toDateMathParser(); - long gotMillisJoda = jodaDateMath.parse("2014-06-06T12:01:02.123", () -> 0, true, (ZoneId) null).toEpochMilli(); - - assertEquals(gotMillisJoda, gotMillisJava); - -// assertSameDate("2014-06-06T12:01:02.123", "yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSS"); -// assertSameDate("2014-06-06T12:01:02.123", "strictDateTimeNoMillis||yyyy-MM-dd'T'HH:mm:ss.SSS"); -// assertSameDate("2014-06-06T12:01:02.123", "yyyy-MM-dd'T'HH:mm:ss+HH:MM||yyyy-MM-dd'T'HH:mm:ss.SSS"); + public void testExceptionWhenCompositeParsingFailsDateMath(){ + //both parsing failures should contain pattern and input text in exception + // both patterns fail parsing the input text due to only 2 digits of millis. Hence full text was not parsed. + String pattern = "yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SS"; + String text = "2014-06-06T12:01:02.123"; + ElasticsearchParseException e1 = expectThrows(ElasticsearchParseException.class, () -> dateMathToMillis(text, DateFormatter.forPattern(pattern))); + assertThat(e1.getMessage(), containsString(pattern)); + assertThat(e1.getMessage(), containsString(text)); + + ElasticsearchParseException e2 = expectThrows(ElasticsearchParseException.class, () -> dateMathToMillis(text, Joda.forPattern(pattern))); + assertThat(e2.getMessage(), containsString(pattern)); + assertThat(e2.getMessage(), containsString(text)); } -// publi + private long dateMathToMillis(String text, DateFormatter dateFormatter) { + DateFormatter javaFormatter = dateFormatter.withLocale(randomLocale(random())); + DateMathParser javaDateMath = javaFormatter.toDateMathParser(); + return javaDateMath.parse(text, () -> 0, true, (ZoneId) null).toEpochMilli(); + } public void testDayOfWeek() { //7 (ok joda) vs 1 (java by default) but 7 with customized org.elasticsearch.common.time.IsoLocale.ISO8601 @@ -873,4 +881,11 @@ private void assertJavaTimeParseException(String input, String format) { assertThat(e.getMessage(), containsString(input)); assertThat(e.getMessage(), containsString(format)); } + + private void assertDateMathEquals(String text, String pattern) { + long gotMillisJava = dateMathToMillis(text, DateFormatter.forPattern(pattern)); + long gotMillisJoda = dateMathToMillis(text, Joda.forPattern(pattern)); + + assertEquals(gotMillisJoda, gotMillisJava); + } } From ee72056035898b0d320a6f90074a8abaf1610551 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Thu, 12 Sep 2019 13:08:01 +0200 Subject: [PATCH 04/12] assertions --- .../elasticsearch/common/joda/JavaJodaTimeDuellingTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java index c1baabef48b8f..030d63c52fad9 100644 --- a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java +++ b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java @@ -56,7 +56,7 @@ public void testCompositeDateMathParsing(){ public void testExceptionWhenCompositeParsingFailsDateMath(){ //both parsing failures should contain pattern and input text in exception - // both patterns fail parsing the input text due to only 2 digits of millis. Hence full text was not parsed. + //both patterns fail parsing the input text due to only 2 digits of millis. Hence full text was not parsed. String pattern = "yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SS"; String text = "2014-06-06T12:01:02.123"; ElasticsearchParseException e1 = expectThrows(ElasticsearchParseException.class, () -> dateMathToMillis(text, DateFormatter.forPattern(pattern))); From 16068f46422f76b7ee20921bad68e4845bee7e3e Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Thu, 12 Sep 2019 13:31:19 +0200 Subject: [PATCH 05/12] remove resolver style and y-u --- .../common/time/DateFormatters.java | 92 +++++++++---------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java index f0fcc868fe2e2..35937b43b03ad 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java @@ -322,7 +322,7 @@ public class DateFormatters { /* * Returns a basic formatter that combines a basic date and time, separated - * by a 'T' (uuuuMMdd'T'HHmmss.SSSZ). + * by a 'T' (yyyyMMdd'T'HHmmss.SSSZ). */ private static final DateFormatter BASIC_DATE_TIME = new JavaDateFormatter("basic_date_time", new DateTimeFormatterBuilder().append(BASIC_DATE_TIME_PRINTER).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), @@ -335,7 +335,7 @@ public class DateFormatters { /* * Returns a basic formatter that combines a basic date and time without millis, - * separated by a 'T' (uuuuMMdd'T'HHmmssZ). + * separated by a 'T' (yyyyMMdd'T'HHmmssZ). */ private static final DateFormatter BASIC_DATE_TIME_NO_MILLIS = new JavaDateFormatter("basic_date_time_no_millis", new DateTimeFormatterBuilder().append(BASIC_DATE_T).append(BASIC_TIME_NO_MILLIS_BASE) @@ -348,39 +348,39 @@ public class DateFormatters { /* * Returns a formatter for a full ordinal date, using a four - * digit year and three digit dayOfYear (uuuuDDD). + * digit year and three digit dayOfYear (yyyyDDD). */ private static final DateFormatter BASIC_ORDINAL_DATE = new JavaDateFormatter("basic_ordinal_date", - DateTimeFormatter.ofPattern("uuuuDDD", IsoLocale.ROOT)); + DateTimeFormatter.ofPattern("yyyyDDD", IsoLocale.ROOT)); /* * Returns a formatter for a full ordinal date and time, using a four - * digit year and three digit dayOfYear (uuuuDDD'T'HHmmss.SSSZ). + * digit year and three digit dayOfYear (yyyyDDD'T'HHmmss.SSSZ). */ private static final DateFormatter BASIC_ORDINAL_DATE_TIME = new JavaDateFormatter("basic_ordinal_date_time", - new DateTimeFormatterBuilder().appendPattern("uuuuDDD").append(BASIC_T_TIME_PRINTER) - .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) - .withResolverStyle(ResolverStyle.STRICT), - new DateTimeFormatterBuilder().appendPattern("uuuuDDD").append(BASIC_T_TIME_FORMATTER) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) - .withResolverStyle(ResolverStyle.STRICT), - new DateTimeFormatterBuilder().appendPattern("uuuuDDD").append(BASIC_T_TIME_FORMATTER) + new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_PRINTER) + .appendOffset("+HH:MM", "Z") + .toFormatter(IsoLocale.ROOT), + new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_FORMATTER) + .appendZoneOrOffsetId() + .toFormatter(IsoLocale.ROOT), + new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_FORMATTER) .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) ); /* * Returns a formatter for a full ordinal date and time without millis, - * using a four digit year and three digit dayOfYear (uuuuDDD'T'HHmmssZ). + * using a four digit year and three digit dayOfYear (yyyyDDD'T'HHmmssZ). */ private static final DateFormatter BASIC_ORDINAL_DATE_TIME_NO_MILLIS = new JavaDateFormatter("basic_ordinal_date_time_no_millis", - new DateTimeFormatterBuilder().appendPattern("uuuuDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) - .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) - .withResolverStyle(ResolverStyle.STRICT), - new DateTimeFormatterBuilder().appendPattern("uuuuDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) - .withResolverStyle(ResolverStyle.STRICT), - new DateTimeFormatterBuilder().appendPattern("uuuuDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) + new DateTimeFormatterBuilder().appendPattern("yyyyDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) + .appendOffset("+HH:MM", "Z") + .toFormatter(IsoLocale.ROOT), + new DateTimeFormatterBuilder().appendPattern("yyyyDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) + .appendZoneOrOffsetId() + .toFormatter(IsoLocale.ROOT), + new DateTimeFormatterBuilder().appendPattern("yyyyDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) ); @@ -496,13 +496,13 @@ public class DateFormatters { * A date formatter that formats or parses a date plus an hour without an offset, such as '2011-12-03T01'. */ private static final DateFormatter STRICT_DATE_HOUR = new JavaDateFormatter("strict_date_hour", - DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH", IsoLocale.ROOT)); + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH", IsoLocale.ROOT)); /* * A date formatter that formats or parses a date plus an hour/minute without an offset, such as '2011-12-03T01:10'. */ private static final DateFormatter STRICT_DATE_HOUR_MINUTE = new JavaDateFormatter("strict_date_hour_minute", - DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm", IsoLocale.ROOT)); + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm", IsoLocale.ROOT)); /* * A strict date formatter that formats or parses a date without an offset, such as '2011-12-03'. @@ -552,7 +552,7 @@ public class DateFormatters { /* * Returns a formatter that combines a full date and time, separated by a 'T' - * (uuuu-MM-dd'T'HH:mm:ss.SSSZZ). + * (yyyy-MM-dd'T'HH:mm:ss.SSSZZ). */ private static final DateFormatter STRICT_DATE_TIME = new JavaDateFormatter("strict_date_time", STRICT_DATE_PRINTER, new DateTimeFormatterBuilder().append(STRICT_DATE_FORMATTER).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), @@ -570,7 +570,7 @@ public class DateFormatters { /* * Returns a formatter for a full ordinal date and time without millis, - * using a four digit year and three digit dayOfYear (uuuu-DDD'T'HH:mm:ssZZ). + * using a four digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ssZZ). */ private static final DateFormatter STRICT_ORDINAL_DATE_TIME_NO_MILLIS = new JavaDateFormatter("strict_ordinal_date_time_no_millis", new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_NO_MILLIS_BASE) @@ -589,7 +589,7 @@ public class DateFormatters { /* * Returns a formatter that combines a full date and time without millis, - * separated by a 'T' (uuuu-MM-dd'T'HH:mm:ssZZ). + * separated by a 'T' (yyyy-MM-dd'T'HH:mm:ssZZ). */ private static final DateFormatter STRICT_DATE_TIME_NO_MILLIS = new JavaDateFormatter("strict_date_time_no_millis", new DateTimeFormatterBuilder().append(STRICT_DATE_TIME_NO_MILLIS_FORMATTER) @@ -630,7 +630,7 @@ public class DateFormatters { /* * Returns a formatter that combines a full date, two digit hour of day, * two digit minute of hour, two digit second of minute, and three digit - * fraction of second (uuuu-MM-dd'T'HH:mm:ss.SSS). + * fraction of second (yyyy-MM-dd'T'HH:mm:ss.SSS). */ private static final DateFormatter STRICT_DATE_HOUR_MINUTE_SECOND_FRACTION = new JavaDateFormatter( "strict_date_hour_minute_second_fraction", @@ -705,7 +705,7 @@ public class DateFormatters { /* * Returns a formatter for a full ordinal date and time, using a four - * digit year and three digit dayOfYear (uuuu-DDD'T'HH:mm:ss.SSSZZ). + * digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ss.SSSZZ). */ private static final DateFormatter STRICT_ORDINAL_DATE_TIME = new JavaDateFormatter("strict_ordinal_date_time", new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_PRINTER) @@ -876,14 +876,14 @@ public class DateFormatters { /* * Returns a formatter that combines a full date, two digit hour of day, * two digit minute of hour, and two digit second of - * minute. (uuuu-MM-dd'T'HH:mm:ss) + * minute. (yyyy-MM-dd'T'HH:mm:ss) */ private static final DateFormatter STRICT_DATE_HOUR_MINUTE_SECOND = new JavaDateFormatter("strict_date_hour_minute_second", - DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss", IsoLocale.ROOT)); + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", IsoLocale.ROOT)); /* * A basic formatter for a full date as four digit year, two digit - * month of year, and two digit day of month (uuuuMMdd). + * month of year, and two digit day of month (yyyyMMdd). */ private static final DateFormatter BASIC_DATE = new JavaDateFormatter("basic_date", new DateTimeFormatterBuilder() @@ -908,7 +908,7 @@ public class DateFormatters { /* * Returns a formatter for a full ordinal date, using a four - * digit year and three digit dayOfYear (uuuu-DDD). + * digit year and three digit dayOfYear (yyyy-DDD). */ private static final DateFormatter STRICT_ORDINAL_DATE = new JavaDateFormatter("strict_ordinal_date", STRICT_ORDINAL_DATE_FORMATTER); @@ -942,7 +942,7 @@ public class DateFormatters { /* * a date formatter with optional time, being very lenient, format is - * uuuu-MM-dd'T'HH:mm:ss.SSSZ + * yyyy-MM-dd'T'HH:mm:ss.SSSZ */ private static final DateFormatter DATE_OPTIONAL_TIME = new JavaDateFormatter("date_optional_time", STRICT_DATE_OPTIONAL_TIME_PRINTER, @@ -1011,7 +1011,7 @@ public class DateFormatters { /* * Returns a formatter for a full ordinal date, using a four - * digit year and three digit dayOfYear (uuuu-DDD). + * digit year and three digit dayOfYear (yyyy-DDD). */ private static final DateFormatter ORDINAL_DATE = new JavaDateFormatter("ordinal_date", ORDINAL_DATE_PRINTER, ORDINAL_DATE_FORMATTER); @@ -1047,17 +1047,17 @@ public class DateFormatters { new DateTimeFormatterBuilder().appendValue(WeekFields.ISO.weekBasedYear()).toFormatter(IsoLocale.ROOT)); /* - * Returns a formatter for a four digit weekyear. (uuuu) + * Returns a formatter for a four digit weekyear. (yyyy) */ private static final DateFormatter YEAR = new JavaDateFormatter("year", new DateTimeFormatterBuilder().appendValue(ChronoField.YEAR).toFormatter(IsoLocale.ROOT)); /* * Returns a formatter that combines a full date and two digit hour of - * day. (uuuu-MM-dd'T'HH) + * day. (yyyy-MM-dd'T'HH) */ private static final DateFormatter DATE_HOUR = new JavaDateFormatter("date_hour", - DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH", IsoLocale.ROOT), + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH", IsoLocale.ROOT), new DateTimeFormatterBuilder() .append(DATE_FORMATTER) .appendLiteral("T") @@ -1067,7 +1067,7 @@ public class DateFormatters { /* * Returns a formatter that combines a full date, two digit hour of day, * two digit minute of hour, two digit second of minute, and three digit - * fraction of second (uuuu-MM-dd'T'HH:mm:ss.SSS). + * fraction of second (yyyy-MM-dd'T'HH:mm:ss.SSS). */ private static final DateFormatter DATE_HOUR_MINUTE_SECOND_MILLIS = new JavaDateFormatter("date_hour_minute_second_millis", @@ -1097,10 +1097,10 @@ public class DateFormatters { /* * Returns a formatter that combines a full date, two digit hour of day, - * and two digit minute of hour. (uuuu-MM-dd'T'HH:mm) + * and two digit minute of hour. (yyyy-MM-dd'T'HH:mm) */ private static final DateFormatter DATE_HOUR_MINUTE = new JavaDateFormatter("date_hour_minute", - DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm", IsoLocale.ROOT), + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm", IsoLocale.ROOT), new DateTimeFormatterBuilder() .append(DATE_FORMATTER) .appendLiteral("T") @@ -1110,10 +1110,10 @@ public class DateFormatters { /* * Returns a formatter that combines a full date, two digit hour of day, * two digit minute of hour, and two digit second of - * minute. (uuuu-MM-dd'T'HH:mm:ss) + * minute. (yyyy-MM-dd'T'HH:mm:ss) */ private static final DateFormatter DATE_HOUR_MINUTE_SECOND = new JavaDateFormatter("date_hour_minute_second", - DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss", IsoLocale.ROOT), + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", IsoLocale.ROOT), new DateTimeFormatterBuilder() .append(DATE_FORMATTER) .appendLiteral("T") @@ -1133,7 +1133,7 @@ public class DateFormatters { /* * Returns a formatter that combines a full date and time, separated by a 'T' - * (uuuu-MM-dd'T'HH:mm:ss.SSSZZ). + * (yyyy-MM-dd'T'HH:mm:ss.SSSZZ). */ private static final DateFormatter DATE_TIME = new JavaDateFormatter("date_time", STRICT_DATE_OPTIONAL_TIME_PRINTER, @@ -1152,7 +1152,7 @@ public class DateFormatters { /* * Returns a formatter for a full date as four digit year, two digit month - * of year, and two digit day of month (uuuu-MM-dd). + * of year, and two digit day of month (yyyy-MM-dd). */ private static final DateFormatter DATE = new JavaDateFormatter("date", DateTimeFormatter.ISO_LOCAL_DATE.withResolverStyle(ResolverStyle.LENIENT), @@ -1180,7 +1180,7 @@ public class DateFormatters { /* * Returns a formatter that combines a full date and time without millis, but with a timezone that can be optional - * separated by a 'T' (uuuu-MM-dd'T'HH:mm:ssZ). + * separated by a 'T' (yyyy-MM-dd'T'HH:mm:ssZ). */ private static final DateFormatter DATE_TIME_NO_MILLIS = new JavaDateFormatter("date_time_no_millis", DATE_TIME_NO_MILLIS_PRINTER, @@ -1244,7 +1244,7 @@ public class DateFormatters { /* * Returns a formatter for a full ordinal date and time, using a four - * digit year and three digit dayOfYear (uuuu-DDD'T'HH:mm:ss.SSSZZ). + * digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ss.SSSZZ). */ private static final DateFormatter ORDINAL_DATE_TIME = new JavaDateFormatter("ordinal_date_time", new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_PRINTER) @@ -1263,7 +1263,7 @@ public class DateFormatters { /* * Returns a formatter for a full ordinal date and time without millis, - * using a four digit year and three digit dayOfYear (uuuu-DDD'T'HH:mm:ssZZ). + * using a four digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ssZZ). */ private static final DateFormatter ORDINAL_DATE_TIME_NO_MILLIS = new JavaDateFormatter("ordinal_date_time_no_millis", new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_NO_MILLIS_BASE) From 78d45c38b9cfd3e6e21b810193c8ae0968753b0f Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Thu, 12 Sep 2019 13:56:51 +0200 Subject: [PATCH 06/12] change formatting --- .../elasticsearch/common/time/DateFormatters.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java index 35937b43b03ad..d3bf066e18481 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java @@ -359,11 +359,9 @@ public class DateFormatters { */ private static final DateFormatter BASIC_ORDINAL_DATE_TIME = new JavaDateFormatter("basic_ordinal_date_time", new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_PRINTER) - .appendOffset("+HH:MM", "Z") - .toFormatter(IsoLocale.ROOT), + .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_FORMATTER) - .appendZoneOrOffsetId() - .toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_FORMATTER) .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) @@ -375,11 +373,9 @@ public class DateFormatters { */ private static final DateFormatter BASIC_ORDINAL_DATE_TIME_NO_MILLIS = new JavaDateFormatter("basic_ordinal_date_time_no_millis", new DateTimeFormatterBuilder().appendPattern("yyyyDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) - .appendOffset("+HH:MM", "Z") - .toFormatter(IsoLocale.ROOT), + .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), new DateTimeFormatterBuilder().appendPattern("yyyyDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) - .appendZoneOrOffsetId() - .toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), new DateTimeFormatterBuilder().appendPattern("yyyyDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) ); @@ -1047,7 +1043,7 @@ public class DateFormatters { new DateTimeFormatterBuilder().appendValue(WeekFields.ISO.weekBasedYear()).toFormatter(IsoLocale.ROOT)); /* - * Returns a formatter for a four digit weekyear. (yyyy) + * Returns a formatter for a four digit year. (uuuu) */ private static final DateFormatter YEAR = new JavaDateFormatter("year", new DateTimeFormatterBuilder().appendValue(ChronoField.YEAR).toFormatter(IsoLocale.ROOT)); From c845d55c189c94202498e10215a95da9d2294592 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Thu, 12 Sep 2019 14:40:48 +0200 Subject: [PATCH 07/12] constructors cleanup --- .../common/time/DateFormatter.java | 2 +- .../elasticsearch/common/time/EpochTime.java | 4 +- .../common/time/JavaDateFormatter.java | 95 +++++++++---------- 3 files changed, 49 insertions(+), 52 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java b/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java index 367aecb74996b..3798cf1744bda 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java @@ -149,6 +149,6 @@ static DateFormatter forPattern(String input) { return formatters.get(0); } - return new JavaDateFormatter(input, formatters); + return JavaDateFormatter.combined(input, formatters); } } diff --git a/server/src/main/java/org/elasticsearch/common/time/EpochTime.java b/server/src/main/java/org/elasticsearch/common/time/EpochTime.java index e718ef40040ad..5875e8572aebb 100644 --- a/server/src/main/java/org/elasticsearch/common/time/EpochTime.java +++ b/server/src/main/java/org/elasticsearch/common/time/EpochTime.java @@ -148,11 +148,11 @@ public long getFrom(TemporalAccessor temporal) { .toFormatter(IsoLocale.ROOT); static final DateFormatter SECONDS_FORMATTER = new JavaDateFormatter("epoch_second", SECONDS_FORMATTER1, - builder -> builder.parseDefaulting(ChronoField.NANO_OF_SECOND, 999_999_999L),null, + builder -> builder.parseDefaulting(ChronoField.NANO_OF_SECOND, 999_999_999L), SECONDS_FORMATTER1, SECONDS_FORMATTER2); static final DateFormatter MILLIS_FORMATTER = new JavaDateFormatter("epoch_millis", MILLISECONDS_FORMATTER1, - builder -> builder.parseDefaulting(EpochTime.NANOS_OF_MILLI, 999_999L),null, + builder -> builder.parseDefaulting(EpochTime.NANOS_OF_MILLI, 999_999L), MILLISECONDS_FORMATTER1, MILLISECONDS_FORMATTER2); private abstract static class EpochField implements TemporalField { 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 5ae6ec5a569b1..964bac0f5cd42 100644 --- a/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java @@ -60,65 +60,36 @@ class JavaDateFormatter implements DateFormatter { private final List parsers; private final JavaDateFormatter roundupParser; + + static class RoundUpFormatter extends JavaDateFormatter{ - private final List roundUpParsers; RoundUpFormatter(String format, List roundUpParsers) { - super(format, null,null, roundUpParsers); - this.roundUpParsers = roundUpParsers; + super(format, firstFrom(roundUpParsers),null, roundUpParsers); } - @Override - public ZoneId zone() { - return roundUpParsers.get(0).getZone(); + private static DateTimeFormatter firstFrom(List roundUpParsers) { + if(roundUpParsers != null && roundUpParsers.size()>0){ + return roundUpParsers.get(0); + } + return null; } @Override - public Locale locale() { - return roundUpParsers.get(0).getLocale(); + JavaDateFormatter getRoundupParser() { + throw new UnsupportedOperationException("RoundUpFormatter does not have another roundUpFormatter"); } } - // named formatters + // named formatters use default roundUpParser JavaDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter... parsers) { - this(format, printer, builder -> ROUND_UP_BASE_FIELDS.forEach(builder::parseDefaulting), null, parsers); + this(format, printer, builder -> ROUND_UP_BASE_FIELDS.forEach(builder::parseDefaulting), parsers); } - //merging - JavaDateFormatter(String pattern, List formatters) { - assert formatters.size() > 0; - - List dateTimeFormatters = new ArrayList<>(formatters.size()); - List roundUpFormatters = new ArrayList<>(formatters.size()); - DateTimeFormatter printer = null; - for (DateFormatter formatter : formatters) { - assert formatter instanceof JavaDateFormatter; - JavaDateFormatter javaDateFormatter = (JavaDateFormatter) formatter; - if (printer == null) { - printer = javaDateFormatter.getPrinter(); - } - dateTimeFormatters.addAll(javaDateFormatter.getParsers()); - roundUpFormatters.addAll(javaDateFormatter.getRoundupParser().getParsers()); - } - - this.format = pattern; - this.printer = printer; - this.roundupParser = roundUpFormatters!=null ? new RoundUpFormatter(format, roundUpFormatters ) : null; - this.parsers = dateTimeFormatters; - } - - // zone & locale and roundup - JavaDateFormatter(String format, DateTimeFormatter printer, List roundUpParsers, List parsers) { - this.format = format; - this.printer = printer; - this.roundupParser = roundUpParsers!=null ? new RoundUpFormatter(format, roundUpParsers ) : null; - this.parsers = parsers; - } - // subclasses + // subclasses override roundUpParser JavaDateFormatter(String format, DateTimeFormatter printer, Consumer roundupParserConsumer, - List roundUpParsers, DateTimeFormatter... parsers) { if (printer == null) { throw new IllegalArgumentException("printer may not be null"); @@ -139,21 +110,47 @@ public Locale locale() { } else { this.parsers = Arrays.asList(parsers); } - - List roundUp = createRoundUpParsers(format,roundupParserConsumer, roundUpParsers); + //this is when the RoundUp Formatter is created. In further merges (with ||) it will only append this one to a list. + List roundUp = createRoundUpParser(format, roundupParserConsumer); this.roundupParser = new RoundUpFormatter(format, roundUp) ; } - private List createRoundUpParsers(String format, - Consumer roundupParserConsumer, - List roundUpParsers) { + private List createRoundUpParser(String format, + Consumer roundupParserConsumer) { if (format.contains("||") == false) { DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder(); builder.append(this.parsers.get(0)); roundupParserConsumer.accept(builder); return Arrays.asList(builder.toFormatter(locale())); } - return roundUpParsers; + return null; + } + + public static DateFormatter combined(String input, List formatters) { + assert formatters.size() > 0; + + List parsers = new ArrayList<>(formatters.size()); + List roundUpParsers = new ArrayList<>(formatters.size()); + + DateTimeFormatter printer = null; + for (DateFormatter formatter : formatters) { + assert formatter instanceof JavaDateFormatter; + JavaDateFormatter javaDateFormatter = (JavaDateFormatter) formatter; + if (printer == null) { + printer = javaDateFormatter.getPrinter(); + } + parsers.addAll(javaDateFormatter.getParsers()); + roundUpParsers.addAll(javaDateFormatter.getRoundupParser().getParsers()); + } + + return new JavaDateFormatter(input, printer, roundUpParsers, parsers); + } + + private JavaDateFormatter(String format, DateTimeFormatter printer, List roundUpParsers, List parsers) { + this.format = format; + this.printer = printer; + this.roundupParser = roundUpParsers != null ? new RoundUpFormatter(format, roundUpParsers ) : null; + this.parsers = parsers; } JavaDateFormatter getRoundupParser() { @@ -259,7 +256,7 @@ public ZoneId zone() { @Override public DateMathParser toDateMathParser() { - return new JavaDateMathParser(format, this, roundupParser); + return new JavaDateMathParser(format, this, getRoundupParser()); } @Override From 0bfede372688a124250f9aa832ca021979cbf1af Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Thu, 12 Sep 2019 15:27:23 +0200 Subject: [PATCH 08/12] checkstyle --- .../org/elasticsearch/common/time/JavaDateFormatter.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 964bac0f5cd42..a247144f544a2 100644 --- a/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java @@ -60,8 +60,6 @@ class JavaDateFormatter implements DateFormatter { private final List parsers; private final JavaDateFormatter roundupParser; - - static class RoundUpFormatter extends JavaDateFormatter{ RoundUpFormatter(String format, List roundUpParsers) { @@ -146,7 +144,8 @@ public static DateFormatter combined(String input, List formatter return new JavaDateFormatter(input, printer, roundUpParsers, parsers); } - private JavaDateFormatter(String format, DateTimeFormatter printer, List roundUpParsers, List parsers) { + private JavaDateFormatter(String format, DateTimeFormatter printer, List roundUpParsers, + List parsers) { this.format = format; this.printer = printer; this.roundupParser = roundUpParsers != null ? new RoundUpFormatter(format, roundUpParsers ) : null; From 3516a19423918a19173c6dbbdae9ca2bf02a7cfa Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Thu, 12 Sep 2019 17:21:51 +0200 Subject: [PATCH 09/12] checkstyle --- .../common/joda/JavaJodaTimeDuellingTests.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java index 030d63c52fad9..a4f5ba637c764 100644 --- a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java +++ b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java @@ -59,11 +59,13 @@ public void testExceptionWhenCompositeParsingFailsDateMath(){ //both patterns fail parsing the input text due to only 2 digits of millis. Hence full text was not parsed. String pattern = "yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SS"; String text = "2014-06-06T12:01:02.123"; - ElasticsearchParseException e1 = expectThrows(ElasticsearchParseException.class, () -> dateMathToMillis(text, DateFormatter.forPattern(pattern))); + ElasticsearchParseException e1 = expectThrows(ElasticsearchParseException.class, + () -> dateMathToMillis(text, DateFormatter.forPattern(pattern))); assertThat(e1.getMessage(), containsString(pattern)); assertThat(e1.getMessage(), containsString(text)); - ElasticsearchParseException e2 = expectThrows(ElasticsearchParseException.class, () -> dateMathToMillis(text, Joda.forPattern(pattern))); + ElasticsearchParseException e2 = expectThrows(ElasticsearchParseException.class, + () -> dateMathToMillis(text, Joda.forPattern(pattern))); assertThat(e2.getMessage(), containsString(pattern)); assertThat(e2.getMessage(), containsString(text)); } From 78d3ee780259ca5623927bfcc36890e0677dfa92 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Thu, 12 Sep 2019 17:33:54 +0200 Subject: [PATCH 10/12] small cleanup --- .../common/time/JavaDateMathParser.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java b/server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java index 5ab857664046a..ead36d1cf0d10 100644 --- a/server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java +++ b/server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java @@ -48,11 +48,11 @@ public class JavaDateMathParser implements DateMathParser { private final JavaDateFormatter formatter; private final String format; - private JavaDateFormatter roundupParser2; + private final JavaDateFormatter roundupParser; - JavaDateMathParser(String format, JavaDateFormatter formatter, JavaDateFormatter roundupParser2) { + JavaDateMathParser(String format, JavaDateFormatter formatter, JavaDateFormatter roundupParser) { this.format = format; - this.roundupParser2 = roundupParser2; + this.roundupParser = roundupParser; Objects.requireNonNull(formatter); this.formatter = formatter; } @@ -216,12 +216,12 @@ private Instant parseDateTime(String value, ZoneId timeZone, boolean roundUpIfNo throw new ElasticsearchParseException("cannot parse empty date"); } - Function formatter = roundUpIfNoTime ? this.roundupParser2::parse : this.formatter::parse; + DateFormatter formatter = roundUpIfNoTime ? this.roundupParser : this.formatter; try { if (timeZone == null) { - return DateFormatters.from(formatter.apply(value)).toInstant(); + return DateFormatters.from(formatter.parse(value)).toInstant(); } else { - TemporalAccessor accessor = formatter.apply(value); + TemporalAccessor accessor = formatter.parse(value); ZoneId zoneId = TemporalQueries.zone().queryFrom(accessor); if (zoneId != null) { timeZone = zoneId; From 9b9df1f2ce58e8ca16c2a5e264bba7e6ca48b490 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Thu, 12 Sep 2019 17:59:50 +0200 Subject: [PATCH 11/12] unused import --- .../java/org/elasticsearch/common/time/JavaDateMathParser.java | 1 - 1 file changed, 1 deletion(-) diff --git a/server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java b/server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java index ead36d1cf0d10..b3fd8fa0f277e 100644 --- a/server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java +++ b/server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java @@ -34,7 +34,6 @@ import java.time.temporal.TemporalAdjusters; import java.time.temporal.TemporalQueries; import java.util.Objects; -import java.util.function.Function; import java.util.function.LongSupplier; /** From 5087225cfdc3e428e3db1ec1c024f9e60fbc5e85 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Fri, 27 Sep 2019 08:51:50 +0200 Subject: [PATCH 12/12] remove unnecessary nullcheck --- .../org/elasticsearch/common/time/JavaDateFormatter.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) 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 a247144f544a2..e1ac6db119609 100644 --- a/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java @@ -67,10 +67,7 @@ static class RoundUpFormatter extends JavaDateFormatter{ } private static DateTimeFormatter firstFrom(List roundUpParsers) { - if(roundUpParsers != null && roundUpParsers.size()>0){ - return roundUpParsers.get(0); - } - return null; + return roundUpParsers.get(0); } @Override