From d31a3e8f220806004eee43ebc4427c49aef3b0f1 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Wed, 11 Sep 2019 17:24:33 +0200 Subject: [PATCH 01/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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 9edf953e92a67675e1e3e8a7c990bac0c13d0c37 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Thu, 12 Sep 2019 16:58:40 +0200 Subject: [PATCH 09/19] year of era support --- .../common/time/DateFormatters.java | 632 ++++++++++++------ .../joda/JavaJodaTimeDuellingTests.java | 4 + 2 files changed, 425 insertions(+), 211 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 d3bf066e18481..6fb77ed9ef5d3 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java @@ -39,6 +39,7 @@ import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAdjusters; import java.time.temporal.TemporalQueries; +import java.time.temporal.TemporalQuery; import java.time.temporal.WeekFields; import static java.time.temporal.ChronoField.DAY_OF_MONTH; @@ -54,7 +55,8 @@ public class DateFormatters { private static final DateTimeFormatter TIME_ZONE_FORMATTER_NO_COLON = new DateTimeFormatterBuilder() .appendOffset("+HHmm", "Z") - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter STRICT_YEAR_MONTH_DAY_FORMATTER = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD) @@ -62,7 +64,8 @@ public class DateFormatters { .appendValue(MONTH_OF_YEAR, 2, 2, SignStyle.NOT_NEGATIVE) .appendLiteral('-') .appendValue(DAY_OF_MONTH, 2, 2, SignStyle.NOT_NEGATIVE) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter STRICT_HOUR_MINUTE_SECOND_FORMATTER = new DateTimeFormatterBuilder() .appendValue(HOUR_OF_DAY, 2, 2, SignStyle.NOT_NEGATIVE) @@ -70,7 +73,8 @@ public class DateFormatters { .appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE) .appendLiteral(':') .appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter STRICT_DATE_OPTIONAL_TIME_PRINTER = new DateTimeFormatterBuilder() .append(STRICT_YEAR_MONTH_DAY_FORMATTER) @@ -92,7 +96,8 @@ public class DateFormatters { .optionalEnd() .optionalEnd() .optionalEnd() - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter STRICT_DATE_OPTIONAL_TIME_FORMATTER = new DateTimeFormatterBuilder() .append(STRICT_YEAR_MONTH_DAY_FORMATTER) @@ -123,7 +128,8 @@ public class DateFormatters { .optionalEnd() .optionalEnd() .optionalEnd() - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /** * Returns a generic ISO datetime parser where the date is mandatory and the time is optional. @@ -150,7 +156,8 @@ public class DateFormatters { .append(TIME_ZONE_FORMATTER_NO_COLON) .optionalEnd() .optionalEnd() - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter STRICT_DATE_OPTIONAL_TIME_PRINTER_NANOS = new DateTimeFormatterBuilder() .append(STRICT_YEAR_MONTH_DAY_FORMATTER) @@ -172,7 +179,8 @@ public class DateFormatters { .optionalEnd() .optionalEnd() .optionalEnd() - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /** * Returns a generic ISO datetime parser where the date is mandatory and the time is optional with nanosecond resolution. @@ -215,7 +223,8 @@ public class DateFormatters { .append(TIME_ZONE_FORMATTER_NO_COLON) .optionalEnd() .optionalEnd() - .toFormatter(IsoLocale.ROOT)); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT)); ///////////////////////////////////////// // @@ -230,7 +239,8 @@ public class DateFormatters { .appendValue(HOUR_OF_DAY, 2, 2, SignStyle.NOT_NEGATIVE) .appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE) .appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * Returns a basic formatter for a two digit hour of day, two digit minute @@ -238,10 +248,13 @@ public class DateFormatters { */ private static final DateFormatter BASIC_TIME_NO_MILLIS = new JavaDateFormatter("basic_time_no_millis", new DateTimeFormatterBuilder().append(BASIC_TIME_NO_MILLIS_BASE).appendOffset("+HH:MM", "Z") - .toFormatter(IsoLocale.ROOT), - new DateTimeFormatterBuilder().append(BASIC_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), + new DateTimeFormatterBuilder().append(BASIC_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(BASIC_TIME_NO_MILLIS_BASE).append(TIME_ZONE_FORMATTER_NO_COLON) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); private static final DateTimeFormatter BASIC_TIME_FORMATTER = new DateTimeFormatterBuilder() @@ -249,14 +262,16 @@ public class DateFormatters { .appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE) .appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE) .appendFraction(NANO_OF_SECOND, 1, 9, true) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter BASIC_TIME_PRINTER = new DateTimeFormatterBuilder() .appendValue(HOUR_OF_DAY, 2, 2, SignStyle.NOT_NEGATIVE) .appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE) .appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE) .appendFraction(NANO_OF_SECOND, 3, 3, true) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * Returns a basic formatter for a two digit hour of day, two digit minute @@ -265,17 +280,22 @@ public class DateFormatters { */ private static final DateFormatter BASIC_TIME = new JavaDateFormatter("basic_time", new DateTimeFormatterBuilder().append(BASIC_TIME_PRINTER).appendOffset("+HH:MM", "Z") - .toFormatter(IsoLocale.ROOT), - new DateTimeFormatterBuilder().append(BASIC_TIME_FORMATTER).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), + new DateTimeFormatterBuilder().append(BASIC_TIME_FORMATTER).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(BASIC_TIME_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); private static final DateTimeFormatter BASIC_T_TIME_PRINTER = - new DateTimeFormatterBuilder().appendLiteral("T").append(BASIC_TIME_PRINTER).toFormatter(IsoLocale.ROOT); + new DateTimeFormatterBuilder().appendLiteral("T").append(BASIC_TIME_PRINTER).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter BASIC_T_TIME_FORMATTER = - new DateTimeFormatterBuilder().appendLiteral("T").append(BASIC_TIME_FORMATTER).toFormatter(IsoLocale.ROOT); + new DateTimeFormatterBuilder().appendLiteral("T").append(BASIC_TIME_FORMATTER).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * Returns a basic formatter for a two digit hour of day, two digit minute @@ -283,8 +303,10 @@ public class DateFormatters { * offset prefixed by 'T' ('T'HHmmss.SSSZ). */ private static final DateFormatter BASIC_T_TIME = new JavaDateFormatter("basic_t_time", - new DateTimeFormatterBuilder().append(BASIC_T_TIME_PRINTER).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), - new DateTimeFormatterBuilder().append(BASIC_T_TIME_FORMATTER).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + new DateTimeFormatterBuilder().append(BASIC_T_TIME_PRINTER).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), + new DateTimeFormatterBuilder().append(BASIC_T_TIME_FORMATTER).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(BASIC_T_TIME_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON). toFormatter(IsoLocale.ROOT) ); @@ -296,88 +318,107 @@ public class DateFormatters { */ private static final DateFormatter BASIC_T_TIME_NO_MILLIS = new JavaDateFormatter("basic_t_time_no_millis", new DateTimeFormatterBuilder().appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) - .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), + .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) .append(TIME_ZONE_FORMATTER_NO_COLON) - .toFormatter(IsoLocale.ROOT) + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); private static final DateTimeFormatter BASIC_YEAR_MONTH_DAY_FORMATTER = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4, 4, SignStyle.NORMAL) .appendValue(MONTH_OF_YEAR, 2, 2, SignStyle.NOT_NEGATIVE) .appendValue(DAY_OF_MONTH, 2, 2, SignStyle.NOT_NEGATIVE) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter BASIC_DATE_TIME_FORMATTER = new DateTimeFormatterBuilder() .append(BASIC_YEAR_MONTH_DAY_FORMATTER) .append(BASIC_T_TIME_FORMATTER) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter BASIC_DATE_TIME_PRINTER = new DateTimeFormatterBuilder() .append(BASIC_YEAR_MONTH_DAY_FORMATTER) .append(BASIC_T_TIME_PRINTER) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * 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), - new DateTimeFormatterBuilder().append(BASIC_DATE_TIME_FORMATTER).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + new DateTimeFormatterBuilder().append(BASIC_DATE_TIME_PRINTER).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), + new DateTimeFormatterBuilder().append(BASIC_DATE_TIME_FORMATTER).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(BASIC_DATE_TIME_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); private static final DateTimeFormatter BASIC_DATE_T = - new DateTimeFormatterBuilder().append(BASIC_YEAR_MONTH_DAY_FORMATTER).appendLiteral("T").toFormatter(IsoLocale.ROOT); + new DateTimeFormatterBuilder().append(BASIC_YEAR_MONTH_DAY_FORMATTER).appendLiteral("T").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * 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) - .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), + .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(BASIC_DATE_T).append(BASIC_TIME_NO_MILLIS_BASE) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(BASIC_DATE_T).append(BASIC_TIME_NO_MILLIS_BASE) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* * 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), + .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_FORMATTER) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_FORMATTER) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* * 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) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + 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) + .withResolverStyle(ResolverStyle.STRICT) ); private static final DateTimeFormatter BASIC_WEEK_DATE_FORMATTER = new DateTimeFormatterBuilder() @@ -385,7 +426,8 @@ public class DateFormatters { .appendLiteral("W") .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1, 2, SignStyle.NEVER) .appendValue(ChronoField.DAY_OF_WEEK) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); ///////////////////////////////////////// // @@ -404,7 +446,8 @@ public class DateFormatters { .appendLiteral("W") .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1, 2, SignStyle.NEVER) .appendValue(ChronoField.DAY_OF_WEEK) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter STRICT_BASIC_WEEK_DATE_PRINTER = new DateTimeFormatterBuilder() .parseStrict() @@ -412,7 +455,8 @@ public class DateFormatters { .appendLiteral("W") .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 2, 2, SignStyle.NEVER) .appendValue(ChronoField.DAY_OF_WEEK) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * Returns a basic formatter for a full date as four digit weekyear, two @@ -434,7 +478,8 @@ public class DateFormatters { .appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE) .appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE) .appendOffset("+HH:MM", "Z") - .toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder() .append(STRICT_BASIC_WEEK_DATE_PRINTER) .appendLiteral("T") @@ -442,7 +487,8 @@ public class DateFormatters { .appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE) .appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE) .appendZoneOrOffsetId() - .toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder() .append(STRICT_BASIC_WEEK_DATE_PRINTER) .appendLiteral("T") @@ -451,6 +497,7 @@ public class DateFormatters { .appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE) .append(TIME_ZONE_FORMATTER_NO_COLON) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -459,9 +506,10 @@ public class DateFormatters { */ private static final DateFormatter STRICT_BASIC_WEEK_DATE_TIME = new JavaDateFormatter("strict_basic_week_date_time", new DateTimeFormatterBuilder() - .append(STRICT_BASIC_WEEK_DATE_PRINTER) - .append(DateTimeFormatter.ofPattern("'T'HHmmss.SSSX", IsoLocale.ROOT)) - .toFormatter(IsoLocale.ROOT), + .append(STRICT_BASIC_WEEK_DATE_PRINTER) + .append(DateTimeFormatter.ofPattern("'T'HHmmss.SSSX", IsoLocale.ROOT)) + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder() .append(STRICT_BASIC_WEEK_DATE_FORMATTER) .appendLiteral("T") @@ -470,7 +518,8 @@ public class DateFormatters { .appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE) .appendFraction(NANO_OF_SECOND, 1, 9, true) .appendZoneOrOffsetId() - .toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder() .append(STRICT_BASIC_WEEK_DATE_FORMATTER) .appendLiteral("T") @@ -480,6 +529,7 @@ public class DateFormatters { .appendFraction(NANO_OF_SECOND, 1, 9, true) .append(TIME_ZONE_FORMATTER_NO_COLON) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -492,13 +542,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'. @@ -511,17 +561,19 @@ public class DateFormatters { */ private static final DateFormatter STRICT_YEAR_MONTH = new JavaDateFormatter("strict_year_month", new DateTimeFormatterBuilder() - .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD) - .appendLiteral("-") - .appendValue(MONTH_OF_YEAR, 2, 2, SignStyle.NOT_NEGATIVE) - .toFormatter(IsoLocale.ROOT)); + .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD) + .appendLiteral("-") + .appendValue(MONTH_OF_YEAR, 2, 2, SignStyle.NOT_NEGATIVE) + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT)); /* * A strict formatter that formats or parses a year, such as '2011'. */ private static final DateFormatter STRICT_YEAR = new JavaDateFormatter("strict_year", new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD) - .toFormatter(IsoLocale.ROOT)); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT)); /* * A strict formatter that formats or parses a hour, minute and second, such as '09:43:25'. @@ -535,7 +587,8 @@ public class DateFormatters { .append(STRICT_HOUR_MINUTE_SECOND_FORMATTER) .appendFraction(NANO_OF_SECOND, 3, 9, true) .appendOffset("+HH:MM", "Z") - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter STRICT_DATE_FORMATTER = new DateTimeFormatterBuilder() .append(STRICT_YEAR_MONTH_DAY_FORMATTER) @@ -544,16 +597,19 @@ public class DateFormatters { .optionalStart() .appendFraction(NANO_OF_SECOND, 1, 9, true) .optionalEnd() - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * 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), + new DateTimeFormatterBuilder().append(STRICT_DATE_FORMATTER).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(STRICT_DATE_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); private static final DateTimeFormatter STRICT_ORDINAL_DATE_TIME_NO_MILLIS_BASE = new DateTimeFormatterBuilder() @@ -562,50 +618,60 @@ public class DateFormatters { .appendValue(DAY_OF_YEAR, 3, 3, SignStyle.NOT_NEGATIVE) .appendLiteral('T') .append(STRICT_HOUR_MINUTE_SECOND_FORMATTER) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * 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) - .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), + .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_NO_MILLIS_BASE) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_NO_MILLIS_BASE) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); private static final DateTimeFormatter STRICT_DATE_TIME_NO_MILLIS_FORMATTER = new DateTimeFormatterBuilder() .append(STRICT_YEAR_MONTH_DAY_FORMATTER) .appendLiteral('T') .append(STRICT_HOUR_MINUTE_SECOND_FORMATTER) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * 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) - .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), + .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(STRICT_DATE_TIME_NO_MILLIS_FORMATTER) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(STRICT_DATE_TIME_NO_MILLIS_FORMATTER) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); // NOTE: this is not a strict formatter to retain the joda time based behaviour, even though it's named like this private static final DateTimeFormatter STRICT_HOUR_MINUTE_SECOND_MILLIS_FORMATTER = new DateTimeFormatterBuilder() .append(STRICT_HOUR_MINUTE_SECOND_FORMATTER) .appendFraction(NANO_OF_SECOND, 1, 9, true) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter STRICT_HOUR_MINUTE_SECOND_MILLIS_PRINTER = new DateTimeFormatterBuilder() .append(STRICT_HOUR_MINUTE_SECOND_FORMATTER) .appendFraction(NANO_OF_SECOND, 3, 3, true) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * Returns a formatter for a two digit hour of day, two digit minute of @@ -626,7 +692,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", @@ -634,7 +700,8 @@ public class DateFormatters { .append(STRICT_YEAR_MONTH_DAY_FORMATTER) .appendLiteral("T") .append(STRICT_HOUR_MINUTE_SECOND_MILLIS_PRINTER) - .toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder() .append(STRICT_YEAR_MONTH_DAY_FORMATTER) .appendLiteral("T") @@ -642,6 +709,7 @@ public class DateFormatters { // this one here is lenient as well to retain joda time based bwc compatibility .appendFraction(NANO_OF_SECOND, 1, 9, true) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); private static final DateFormatter STRICT_DATE_HOUR_MINUTE_SECOND_MILLIS = new JavaDateFormatter( @@ -650,7 +718,8 @@ public class DateFormatters { .append(STRICT_YEAR_MONTH_DAY_FORMATTER) .appendLiteral("T") .append(STRICT_HOUR_MINUTE_SECOND_MILLIS_PRINTER) - .toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder() .append(STRICT_YEAR_MONTH_DAY_FORMATTER) .appendLiteral("T") @@ -658,6 +727,7 @@ public class DateFormatters { // this one here is lenient as well to retain joda time based bwc compatibility .appendFraction(NANO_OF_SECOND, 1, 9, true) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -684,7 +754,8 @@ public class DateFormatters { .appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE) .appendFraction(NANO_OF_SECOND, 3, 9, true) .optionalEnd() - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter STRICT_ORDINAL_DATE_TIME_FORMATTER_BASE = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD) @@ -697,19 +768,23 @@ public class DateFormatters { .appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE) .appendFraction(NANO_OF_SECOND, 1, 9, true) .optionalEnd() - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * 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) - .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), + .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_FORMATTER_BASE) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_FORMATTER_BASE) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); // Note: milliseconds parsing is not strict, others are @@ -720,7 +795,8 @@ public class DateFormatters { .appendLiteral(':') .appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE) .appendFraction(NANO_OF_SECOND, 1, 9, true) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter STRICT_TIME_PRINTER = new DateTimeFormatterBuilder() .appendValue(HOUR_OF_DAY, 2, 2, SignStyle.NOT_NEGATIVE) @@ -729,7 +805,8 @@ public class DateFormatters { .appendLiteral(':') .appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE) .appendFraction(NANO_OF_SECOND, 3, 3, true) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * Returns a formatter for a two digit hour of day, two digit minute of @@ -737,10 +814,13 @@ public class DateFormatters { * time zone offset (HH:mm:ss.SSSZZ). */ private static final DateFormatter STRICT_TIME = new JavaDateFormatter("strict_time", - new DateTimeFormatterBuilder().append(STRICT_TIME_PRINTER).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), - new DateTimeFormatterBuilder().append(STRICT_TIME_FORMATTER_BASE).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + new DateTimeFormatterBuilder().append(STRICT_TIME_PRINTER).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), + new DateTimeFormatterBuilder().append(STRICT_TIME_FORMATTER_BASE).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(STRICT_TIME_FORMATTER_BASE).append(TIME_ZONE_FORMATTER_NO_COLON) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -751,11 +831,14 @@ public class DateFormatters { private static final DateFormatter STRICT_T_TIME = new JavaDateFormatter("strict_t_time", new DateTimeFormatterBuilder().appendLiteral('T').append(STRICT_TIME_PRINTER) .appendOffset("+HH:MM", "Z") - .toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().appendLiteral('T').append(STRICT_TIME_FORMATTER_BASE) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().appendLiteral('T').append(STRICT_TIME_FORMATTER_BASE) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); private static final DateTimeFormatter STRICT_TIME_NO_MILLIS_BASE = new DateTimeFormatterBuilder() @@ -764,7 +847,8 @@ public class DateFormatters { .appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE) .appendLiteral(':') .appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * Returns a formatter for a two digit hour of day, two digit minute of @@ -772,10 +856,13 @@ public class DateFormatters { */ private static final DateFormatter STRICT_TIME_NO_MILLIS = new JavaDateFormatter("strict_time_no_millis", new DateTimeFormatterBuilder().append(STRICT_TIME_NO_MILLIS_BASE).appendOffset("+HH:MM", "Z") - .toFormatter(IsoLocale.ROOT), - new DateTimeFormatterBuilder().append(STRICT_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), + new DateTimeFormatterBuilder().append(STRICT_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(STRICT_TIME_NO_MILLIS_BASE).append(TIME_ZONE_FORMATTER_NO_COLON) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -785,26 +872,31 @@ public class DateFormatters { */ private static final DateFormatter STRICT_T_TIME_NO_MILLIS = new JavaDateFormatter("strict_t_time_no_millis", new DateTimeFormatterBuilder().appendLiteral("T").append(STRICT_TIME_NO_MILLIS_BASE) - .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), + .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().appendLiteral("T").append(STRICT_TIME_NO_MILLIS_BASE) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().appendLiteral("T").append(STRICT_TIME_NO_MILLIS_BASE) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); private static final DateTimeFormatter ISO_WEEK_DATE = new DateTimeFormatterBuilder() - .parseCaseInsensitive() - .appendValue(IsoFields.WEEK_BASED_YEAR, 4, 10, SignStyle.EXCEEDS_PAD) - .appendLiteral("-W") - .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 2) - .appendLiteral('-') - .appendValue(DAY_OF_WEEK, 1) - .toFormatter(IsoLocale.ROOT); + .parseCaseInsensitive() + .appendValue(IsoFields.WEEK_BASED_YEAR, 4, 10, SignStyle.EXCEEDS_PAD) + .appendLiteral("-W") + .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 2) + .appendLiteral('-') + .appendValue(DAY_OF_WEEK, 1) + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter ISO_WEEK_DATE_T = new DateTimeFormatterBuilder() - .append(ISO_WEEK_DATE) - .appendLiteral('T') - .toFormatter(IsoLocale.ROOT); + .append(ISO_WEEK_DATE) + .appendLiteral('T') + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * Returns a formatter for a full date as four digit weekyear, two digit @@ -818,11 +910,14 @@ public class DateFormatters { */ private static final DateFormatter STRICT_WEEK_DATE_TIME_NO_MILLIS = new JavaDateFormatter("strict_week_date_time_no_millis", new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T) - .append(STRICT_TIME_NO_MILLIS_BASE).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), + .append(STRICT_TIME_NO_MILLIS_BASE).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T) - .append(STRICT_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .append(STRICT_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T) - .append(STRICT_TIME_NO_MILLIS_BASE).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .append(STRICT_TIME_NO_MILLIS_BASE).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -831,11 +926,14 @@ public class DateFormatters { */ private static final DateFormatter STRICT_WEEK_DATE_TIME = new JavaDateFormatter("strict_week_date_time", new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T) - .append(STRICT_TIME_PRINTER).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), + .append(STRICT_TIME_PRINTER).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T).append(STRICT_TIME_FORMATTER_BASE) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T).append(STRICT_TIME_FORMATTER_BASE) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -843,13 +941,15 @@ public class DateFormatters { */ private static final DateFormatter STRICT_WEEKYEAR = new JavaDateFormatter("strict_weekyear", new DateTimeFormatterBuilder() .appendValue(WeekFields.ISO.weekBasedYear(), 4, 10, SignStyle.EXCEEDS_PAD) - .toFormatter(IsoLocale.ROOT)); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT)); private static final DateTimeFormatter STRICT_WEEKYEAR_WEEK_FORMATTER = new DateTimeFormatterBuilder() .appendValue(WeekFields.ISO.weekBasedYear(), 4, 10, SignStyle.EXCEEDS_PAD) .appendLiteral("-W") .appendValue(WeekFields.ISO.weekOfWeekBasedYear(), 2, 2, SignStyle.NOT_NEGATIVE) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * Returns a formatter for a four digit weekyear and two digit week of @@ -864,34 +964,37 @@ public class DateFormatters { */ private static final DateFormatter STRICT_WEEKYEAR_WEEK_DAY = new JavaDateFormatter("strict_weekyear_week_day", new DateTimeFormatterBuilder() - .append(STRICT_WEEKYEAR_WEEK_FORMATTER) - .appendLiteral("-") - .appendValue(WeekFields.ISO.dayOfWeek()) - .toFormatter(IsoLocale.ROOT)); + .append(STRICT_WEEKYEAR_WEEK_FORMATTER) + .appendLiteral("-") + .appendValue(WeekFields.ISO.dayOfWeek()) + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT)); /* * 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() .appendValue(ChronoField.YEAR, 4, 4, SignStyle.NORMAL) .appendValue(MONTH_OF_YEAR, 2, 2, SignStyle.NOT_NEGATIVE) .appendValue(DAY_OF_MONTH, 2, 2, SignStyle.NOT_NEGATIVE) - .toFormatter(IsoLocale.ROOT).withZone(ZoneOffset.UTC), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT).withZone(ZoneOffset.UTC), new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 1, 4, SignStyle.NORMAL) .appendValue(MONTH_OF_YEAR, 1, 2, SignStyle.NOT_NEGATIVE) .appendValue(DAY_OF_MONTH, 1, 2, SignStyle.NOT_NEGATIVE) - .toFormatter(IsoLocale.ROOT).withZone(ZoneOffset.UTC) + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT).withZone(ZoneOffset.UTC) ); private static final DateTimeFormatter STRICT_ORDINAL_DATE_FORMATTER = new DateTimeFormatterBuilder() @@ -900,11 +1003,12 @@ public class DateFormatters { .appendLiteral('-') .appendValue(DAY_OF_YEAR, 3) .optionalStart() - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * 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); @@ -928,17 +1032,18 @@ public class DateFormatters { .appendLiteral('-') .appendValue(DAY_OF_MONTH, 1, 2, SignStyle.NOT_NEGATIVE) .optionalEnd() - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter HOUR_MINUTE_FORMATTER = new DateTimeFormatterBuilder() .appendValue(HOUR_OF_DAY, 1, 2, SignStyle.NOT_NEGATIVE) .appendLiteral(':') .appendValue(MINUTE_OF_HOUR, 1, 2, SignStyle.NOT_NEGATIVE) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT).withResolverStyle(ResolverStyle.STRICT); /* * 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, @@ -967,13 +1072,14 @@ public class DateFormatters { .optionalEnd() .optionalEnd() .optionalEnd() - .toFormatter(IsoLocale.ROOT)); + .toFormatter(IsoLocale.ROOT).withResolverStyle(ResolverStyle.STRICT)); private static final DateTimeFormatter HOUR_MINUTE_SECOND_FORMATTER = new DateTimeFormatterBuilder() .append(HOUR_MINUTE_FORMATTER) .appendLiteral(":") .appendValue(SECOND_OF_MINUTE, 1, 2, SignStyle.NOT_NEGATIVE) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter HOUR_MINUTE_SECOND_MILLIS_FORMATTER = new DateTimeFormatterBuilder() .appendValue(HOUR_OF_DAY, 1, 2, SignStyle.NOT_NEGATIVE) @@ -982,7 +1088,8 @@ public class DateFormatters { .appendLiteral(':') .appendValue(SECOND_OF_MINUTE, 1, 2, SignStyle.NOT_NEGATIVE) .appendFraction(NANO_OF_SECOND, 1, 3, true) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter HOUR_MINUTE_SECOND_FRACTION_FORMATTER = new DateTimeFormatterBuilder() .appendValue(HOUR_OF_DAY, 1, 2, SignStyle.NOT_NEGATIVE) @@ -991,23 +1098,26 @@ public class DateFormatters { .appendLiteral(':') .appendValue(SECOND_OF_MINUTE, 1, 2, SignStyle.NOT_NEGATIVE) .appendFraction(NANO_OF_SECOND, 1, 9, true) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter ORDINAL_DATE_FORMATTER = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD) .appendLiteral('-') .appendValue(DAY_OF_YEAR, 1, 3, SignStyle.NOT_NEGATIVE) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter ORDINAL_DATE_PRINTER = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD) .appendLiteral('-') .appendValue(DAY_OF_YEAR, 3, 3, SignStyle.NOT_NEGATIVE) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * 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); @@ -1018,15 +1128,18 @@ public class DateFormatters { .appendValue(MINUTE_OF_HOUR, 1, 2, SignStyle.NOT_NEGATIVE) .appendLiteral(':') .appendValue(SECOND_OF_MINUTE, 1, 2, SignStyle.NOT_NEGATIVE) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter T_TIME_NO_MILLIS_FORMATTER = - new DateTimeFormatterBuilder().appendLiteral("T").append(TIME_NO_MILLIS_FORMATTER).toFormatter(IsoLocale.ROOT); + new DateTimeFormatterBuilder().appendLiteral("T").append(TIME_NO_MILLIS_FORMATTER).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter TIME_PREFIX = new DateTimeFormatterBuilder() .append(TIME_NO_MILLIS_FORMATTER) .appendFraction(NANO_OF_SECOND, 1, 9, true) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter WEEK_DATE_FORMATTER = new DateTimeFormatterBuilder() .appendValue(IsoFields.WEEK_BASED_YEAR, 4, 10, SignStyle.EXCEEDS_PAD) @@ -1034,36 +1147,40 @@ public class DateFormatters { .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1, 2, SignStyle.NOT_NEGATIVE) .appendLiteral('-') .appendValue(DAY_OF_WEEK, 1) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * Returns a formatter for a four digit weekyear. (YYYY) */ private static final DateFormatter WEEK_YEAR = new JavaDateFormatter("week_year", - new DateTimeFormatterBuilder().appendValue(WeekFields.ISO.weekBasedYear()).toFormatter(IsoLocale.ROOT)); + new DateTimeFormatterBuilder().appendValue(WeekFields.ISO.weekBasedYear()).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT)); /* - * Returns a formatter for a four digit year. (uuuu) + * Returns a formatter for a four digit weekyear. (uuuu) */ private static final DateFormatter YEAR = new JavaDateFormatter("year", - new DateTimeFormatterBuilder().appendValue(ChronoField.YEAR).toFormatter(IsoLocale.ROOT)); + new DateTimeFormatterBuilder().appendValue(ChronoField.YEAR).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT)); /* * 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") .appendValue(HOUR_OF_DAY, 1, 2, SignStyle.NOT_NEGATIVE) - .toFormatter(IsoLocale.ROOT)); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT)); /* * 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", @@ -1071,12 +1188,14 @@ public class DateFormatters { .append(STRICT_YEAR_MONTH_DAY_FORMATTER) .appendLiteral("T") .append(STRICT_HOUR_MINUTE_SECOND_MILLIS_PRINTER) - .toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder() .append(DATE_FORMATTER) .appendLiteral("T") .append(HOUR_MINUTE_SECOND_MILLIS_FORMATTER) - .toFormatter(IsoLocale.ROOT)); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT)); private static final DateFormatter DATE_HOUR_MINUTE_SECOND_FRACTION = new JavaDateFormatter("date_hour_minute_second_fraction", @@ -1084,37 +1203,41 @@ public class DateFormatters { .append(STRICT_YEAR_MONTH_DAY_FORMATTER) .appendLiteral("T") .append(STRICT_HOUR_MINUTE_SECOND_MILLIS_PRINTER) - .toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder() .append(DATE_FORMATTER) .appendLiteral("T") .append(HOUR_MINUTE_SECOND_FRACTION_FORMATTER) - .toFormatter(IsoLocale.ROOT)); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT)); /* * 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") .append(HOUR_MINUTE_FORMATTER) - .toFormatter(IsoLocale.ROOT)); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT)); /* * 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") .append(HOUR_MINUTE_SECOND_FORMATTER) - .toFormatter(IsoLocale.ROOT)); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT)); private static final DateTimeFormatter DATE_TIME_FORMATTER = new DateTimeFormatterBuilder() .append(DATE_FORMATTER) @@ -1125,18 +1248,21 @@ public class DateFormatters { .appendValue(SECOND_OF_MINUTE, 1, 2, SignStyle.NOT_NEGATIVE) .appendFraction(NANO_OF_SECOND, 1, 9, true) .optionalEnd() - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * 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, new DateTimeFormatterBuilder().append(DATE_TIME_FORMATTER).appendOffset("+HH:MM", "Z") - .toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(DATE_TIME_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -1148,10 +1274,10 @@ 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), + DateTimeFormatter.ISO_LOCAL_DATE.withResolverStyle(ResolverStyle.STRICT), DATE_FORMATTER); // only the formatter, nothing optional here @@ -1162,7 +1288,8 @@ public class DateFormatters { .appendLiteral(':') .appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE) .appendZoneId() - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter DATE_TIME_PREFIX = new DateTimeFormatterBuilder() .append(DATE_FORMATTER) @@ -1172,21 +1299,26 @@ public class DateFormatters { .appendLiteral(':') .appendValue(SECOND_OF_MINUTE, 1, 2, SignStyle.NOT_NEGATIVE) .optionalEnd() - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * 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, - new DateTimeFormatterBuilder().append(DATE_TIME_PREFIX).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), + new DateTimeFormatterBuilder().append(DATE_TIME_PREFIX).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(DATE_TIME_PREFIX).append(TIME_ZONE_FORMATTER_NO_COLON) - .toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(DATE_TIME_PREFIX) - .optionalStart().appendZoneOrOffsetId().optionalEnd().toFormatter(IsoLocale.ROOT), + .optionalStart().appendZoneOrOffsetId().optionalEnd().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(DATE_TIME_PREFIX) - .optionalStart().append(TIME_ZONE_FORMATTER_NO_COLON).optionalEnd().toFormatter(IsoLocale.ROOT) + .optionalStart().append(TIME_ZONE_FORMATTER_NO_COLON).optionalEnd().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -1217,6 +1349,7 @@ public class DateFormatters { .appendLiteral(":") .appendValue(SECOND_OF_MINUTE, 1, 2, SignStyle.NOT_NEGATIVE) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -1225,6 +1358,7 @@ public class DateFormatters { private static final DateFormatter HOUR = new JavaDateFormatter("hour", DateTimeFormatter.ofPattern("HH", IsoLocale.ROOT), new DateTimeFormatterBuilder().appendValue(HOUR_OF_DAY, 1, 2, SignStyle.NOT_NEGATIVE).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); private static final DateTimeFormatter ORDINAL_DATE_TIME_FORMATTER_BASE = new DateTimeFormatterBuilder() @@ -1236,38 +1370,46 @@ public class DateFormatters { .appendValue(SECOND_OF_MINUTE, 1, 2, SignStyle.NOT_NEGATIVE) .appendFraction(NANO_OF_SECOND, 1, 9, true) .optionalEnd() - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * 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) - .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), + .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(ORDINAL_DATE_TIME_FORMATTER_BASE) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(ORDINAL_DATE_TIME_FORMATTER_BASE) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); private static final DateTimeFormatter ORDINAL_DATE_TIME_NO_MILLIS_BASE = new DateTimeFormatterBuilder() .append(ORDINAL_DATE_FORMATTER) .appendLiteral('T') .append(HOUR_MINUTE_SECOND_FORMATTER) - .toFormatter(IsoLocale.ROOT); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); /* * 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) - .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), + .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(ORDINAL_DATE_TIME_NO_MILLIS_BASE) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(ORDINAL_DATE_TIME_NO_MILLIS_BASE) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -1276,11 +1418,14 @@ public class DateFormatters { */ private static final DateFormatter WEEK_DATE_TIME = new JavaDateFormatter("week_date_time", new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T) - .append(STRICT_TIME_PRINTER).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), + .append(STRICT_TIME_PRINTER).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(WEEK_DATE_FORMATTER).appendLiteral("T").append(TIME_PREFIX) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(WEEK_DATE_FORMATTER).appendLiteral("T").append(TIME_PREFIX) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -1289,11 +1434,14 @@ public class DateFormatters { */ private static final DateFormatter WEEK_DATE_TIME_NO_MILLIS = new JavaDateFormatter("week_date_time_no_millis", new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T) - .append(STRICT_TIME_NO_MILLIS_BASE).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), + .append(STRICT_TIME_NO_MILLIS_BASE).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(WEEK_DATE_FORMATTER).append(T_TIME_NO_MILLIS_FORMATTER) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(WEEK_DATE_FORMATTER).append(T_TIME_NO_MILLIS_FORMATTER) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -1304,11 +1452,14 @@ public class DateFormatters { new DateTimeFormatterBuilder() .append(STRICT_BASIC_WEEK_DATE_PRINTER) .append(DateTimeFormatter.ofPattern("'T'HHmmss.SSSX", IsoLocale.ROOT)) - .toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(BASIC_WEEK_DATE_FORMATTER).append(BASIC_T_TIME_FORMATTER) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(BASIC_WEEK_DATE_FORMATTER).append(BASIC_T_TIME_FORMATTER) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -1318,11 +1469,14 @@ public class DateFormatters { private static final DateFormatter BASIC_WEEK_DATE_TIME_NO_MILLIS = new JavaDateFormatter("basic_week_date_time_no_millis", new DateTimeFormatterBuilder() .append(STRICT_BASIC_WEEK_DATE_PRINTER).append(DateTimeFormatter.ofPattern("'T'HHmmssX", IsoLocale.ROOT)) - .toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(BASIC_WEEK_DATE_FORMATTER).appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(BASIC_WEEK_DATE_FORMATTER).appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -1331,9 +1485,12 @@ public class DateFormatters { * time zone offset (HH:mm:ss.SSSZZ). */ private static final DateFormatter TIME = new JavaDateFormatter("time", - new DateTimeFormatterBuilder().append(STRICT_TIME_PRINTER).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), - new DateTimeFormatterBuilder().append(TIME_PREFIX).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + new DateTimeFormatterBuilder().append(STRICT_TIME_PRINTER).appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), + new DateTimeFormatterBuilder().append(TIME_PREFIX).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(TIME_PREFIX).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -1342,10 +1499,13 @@ public class DateFormatters { */ private static final DateFormatter TIME_NO_MILLIS = new JavaDateFormatter("time_no_millis", new DateTimeFormatterBuilder().append(STRICT_TIME_NO_MILLIS_BASE).appendOffset("+HH:MM", "Z") - .toFormatter(IsoLocale.ROOT), - new DateTimeFormatterBuilder().append(TIME_NO_MILLIS_FORMATTER).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), + new DateTimeFormatterBuilder().append(TIME_NO_MILLIS_FORMATTER).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(TIME_NO_MILLIS_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -1355,11 +1515,14 @@ public class DateFormatters { */ private static final DateFormatter T_TIME = new JavaDateFormatter("t_time", new DateTimeFormatterBuilder().appendLiteral('T').append(STRICT_TIME_PRINTER).appendOffset("+HH:MM", "Z") - .toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().appendLiteral("T").append(TIME_PREFIX) - .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().appendLiteral("T").append(TIME_PREFIX) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -1369,10 +1532,13 @@ public class DateFormatters { */ private static final DateFormatter T_TIME_NO_MILLIS = new JavaDateFormatter("t_time_no_millis", new DateTimeFormatterBuilder().appendLiteral("T").append(STRICT_TIME_NO_MILLIS_BASE) - .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT), - new DateTimeFormatterBuilder().append(T_TIME_NO_MILLIS_FORMATTER).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT), + .appendOffset("+HH:MM", "Z").toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), + new DateTimeFormatterBuilder().append(T_TIME_NO_MILLIS_FORMATTER).appendZoneOrOffsetId().toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().append(T_TIME_NO_MILLIS_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -1383,9 +1549,11 @@ public class DateFormatters { .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD) .appendLiteral("-") .appendValue(MONTH_OF_YEAR, 2, 2, SignStyle.NOT_NEGATIVE) - .toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder().appendValue(ChronoField.YEAR).appendLiteral("-").appendValue(MONTH_OF_YEAR) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -1400,6 +1568,7 @@ public class DateFormatters { .appendLiteral("-") .appendValue(DAY_OF_MONTH) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -1418,6 +1587,7 @@ public class DateFormatters { .appendLiteral("-W") .appendValue(WeekFields.ISO.weekOfWeekBasedYear()) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); /* @@ -1429,7 +1599,8 @@ public class DateFormatters { .append(STRICT_WEEKYEAR_WEEK_FORMATTER) .appendLiteral("-") .appendValue(WeekFields.ISO.dayOfWeek()) - .toFormatter(IsoLocale.ROOT), + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT), new DateTimeFormatterBuilder() .appendValue(WeekFields.ISO.weekBasedYear()) .appendLiteral("-W") @@ -1437,6 +1608,7 @@ public class DateFormatters { .appendLiteral("-") .appendValue(WeekFields.ISO.dayOfWeek()) .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT) ); @@ -1619,7 +1791,8 @@ static DateFormatter forPattern(String input) { try { return new JavaDateFormatter(input, new DateTimeFormatterBuilder() .appendPattern(input) - .toFormatter(IsoLocale.ROOT)); + .toFormatter(IsoLocale.ROOT) + .withResolverStyle(ResolverStyle.STRICT)); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid format: [" + input + "]: " + e.getMessage(), e); } @@ -1667,7 +1840,7 @@ public static ZonedDateTime from(TemporalAccessor accessor) { zoneId = ZoneOffset.UTC; } - LocalDate localDate = accessor.query(TemporalQueries.localDate()); + LocalDate localDate = accessor.query(LOCAL_DATE_QUERY); LocalTime localTime = accessor.query(TemporalQueries.localTime()); boolean isLocalDateSet = localDate != null; boolean isLocalTimeSet = localTime != null; @@ -1681,7 +1854,7 @@ public static ZonedDateTime from(TemporalAccessor accessor) { return localDate.atStartOfDay(zoneId); } else if (isLocalTimeSet) { return of(getLocaldate(accessor), localTime, zoneId); - } else if (accessor.isSupported(ChronoField.YEAR)) { + } else if (accessor.isSupported(ChronoField.YEAR) || accessor.isSupported(ChronoField.YEAR_OF_ERA) ) { if (accessor.isSupported(MONTH_OF_YEAR)) { return getFirstOfMonth(accessor).atStartOfDay(zoneId); } else { @@ -1709,18 +1882,55 @@ public static ZonedDateTime from(TemporalAccessor accessor) { throw new IllegalArgumentException("temporal accessor [" + accessor + "] cannot be converted to zoned date time"); } + /** + * extending the java.time.temporal.TemporalQueries.LOCAL_DATE implementation to also create local dates when YearOfEra was used instead of Year. + * This is to make it compatible with Joda behaviour + */ + static final TemporalQuery LOCAL_DATE_QUERY = new TemporalQuery<>() { + @Override + public LocalDate queryFrom(TemporalAccessor temporal) { + if (temporal.isSupported(ChronoField.EPOCH_DAY)) { + return LocalDate.ofEpochDay(temporal.getLong(ChronoField.EPOCH_DAY)); + } else if( temporal.isSupported(ChronoField.YEAR_OF_ERA) || temporal.isSupported(ChronoField.YEAR)) { + int year = getYear(temporal); + if(temporal.isSupported(ChronoField.MONTH_OF_YEAR) && temporal.isSupported(ChronoField.DAY_OF_MONTH)){ + return LocalDate.of(year, temporal.get(ChronoField.MONTH_OF_YEAR), temporal.get(ChronoField.DAY_OF_MONTH)); + } else if (temporal.isSupported(DAY_OF_YEAR)) { + return LocalDate.ofYearDay(year, temporal.get(DAY_OF_YEAR)); + } + } + return null; + } + + @Override + public String toString() { + return "LocalDate"; + } + }; + private static LocalDate getLocaldate(TemporalAccessor accessor) { + int year = getYear(accessor); if (accessor.isSupported(MONTH_OF_YEAR)) { if (accessor.isSupported(DAY_OF_MONTH)) { - return LocalDate.of(1970, accessor.get(MONTH_OF_YEAR), accessor.get(DAY_OF_MONTH)); + return LocalDate.of(year, accessor.get(MONTH_OF_YEAR), accessor.get(DAY_OF_MONTH)); } else { - return LocalDate.of(1970, accessor.get(MONTH_OF_YEAR), 1); + return LocalDate.of(year, accessor.get(MONTH_OF_YEAR), 1); } } return LOCALDATE_EPOCH; } + private static int getYear(TemporalAccessor accessor) { + if(accessor.isSupported(ChronoField.YEAR)){ + return accessor.get(ChronoField.YEAR); + } + if(accessor.isSupported(ChronoField.YEAR_OF_ERA)){ + return accessor.get(ChronoField.YEAR_OF_ERA); + } + return 1970; + } + @SuppressForbidden(reason = "ZonedDateTime.of is fine here") private static ZonedDateTime of(LocalDate localDate, LocalTime localTime, ZoneId zoneId) { return ZonedDateTime.of(localDate, localTime, zoneId); @@ -1728,6 +1938,6 @@ private static ZonedDateTime of(LocalDate localDate, LocalTime localTime, ZoneId @SuppressForbidden(reason = "LocalDate.of is fine here") private static LocalDate getFirstOfMonth(TemporalAccessor accessor) { - return LocalDate.of(accessor.get(ChronoField.YEAR), accessor.get(MONTH_OF_YEAR), 1); + return LocalDate.of(getYear(accessor), accessor.get(MONTH_OF_YEAR), 1); } } 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..ccd8319777fb4 100644 --- a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java +++ b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java @@ -824,6 +824,10 @@ public void testIso8601Parsers() { assertSameDate("2018-10-10T10:11:12,123+05:30", format, jodaFormatter, javaFormatter); } + public void testParsingLocalDateFromYearOfEra(){ + //with strict resolving, YearOfEra expect an era, otherwise it won't resolve to a date + assertSameDate("2018363","yyyyDDD",Joda.forPattern("YYYYDDD"),DateFormatter.forPattern("uuuuDDD")); + } public void testParsingMissingTimezone() { long millisJava = DateFormatter.forPattern("8yyyy-MM-dd HH:mm:ss").parseMillis("2018-02-18 17:47:17"); long millisJoda = DateFormatter.forPattern("yyyy-MM-dd HH:mm:ss").parseMillis("2018-02-18 17:47:17"); From a7a088bdae344b211c11ebac0c66ec07d3c2ded0 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Thu, 12 Sep 2019 17:18:20 +0200 Subject: [PATCH 10/19] checkstyle --- .../java/org/elasticsearch/common/time/DateFormatters.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 6fb77ed9ef5d3..0b1dd3d33c16c 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java @@ -1883,7 +1883,8 @@ public static ZonedDateTime from(TemporalAccessor accessor) { } /** - * extending the java.time.temporal.TemporalQueries.LOCAL_DATE implementation to also create local dates when YearOfEra was used instead of Year. + * extending the java.time.temporal.TemporalQueries.LOCAL_DATE implementation to also create local dates + * when YearOfEra was used instead of Year. * This is to make it compatible with Joda behaviour */ static final TemporalQuery LOCAL_DATE_QUERY = new TemporalQuery<>() { From 9c169ec47e2370e63939ad2aa39e37e240bebcee Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Thu, 12 Sep 2019 17:26:22 +0200 Subject: [PATCH 11/19] 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 ccd8319777fb4..89d768340569a 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 ae650c2d6414425763c9b476749d8f93137e6045 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Fri, 13 Sep 2019 08:49:41 +0200 Subject: [PATCH 12/19] fix year-of-era parsing for ingest.dateFormat --- .../org/elasticsearch/ingest/common/DateFormat.java | 4 ++-- .../elasticsearch/ingest/common/DateFormatTests.java | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java index be5d7e47f1c02..c4b96bb06b0b2 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java @@ -98,9 +98,9 @@ Function getFunction(String format, ZoneId zoneId, Locale final DateFormatter formatter = dateFormatter; return text -> { TemporalAccessor accessor = formatter.parse(text); - // if there is no year, we fall back to the current one and + // if there is no year nor year-of-era, we fall back to the current one and // fill the rest of the date up with the parsed date - if (accessor.isSupported(ChronoField.YEAR) == false) { + if (accessor.isSupported(ChronoField.YEAR) == false && accessor.isSupported(ChronoField.YEAR_OF_ERA) == false ) { int year = LocalDate.now(ZoneOffset.UTC).getYear(); ZonedDateTime newTime = Instant.EPOCH.atZone(ZoneOffset.UTC).withYear(year); for (ChronoField field : FIELDS) { diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateFormatTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateFormatTests.java index e44e62be8629a..a9adab4e7f5db 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateFormatTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateFormatTests.java @@ -44,7 +44,7 @@ public void testParseJava() { equalTo("11 24 01:29:01")); } - public void testParseJavaWithTimeZone() { + public void testParseYearOfEraJavaWithTimeZone() { Function javaFunction = DateFormat.Java.getFunction("yyyy-MM-dd'T'HH:mm:ss.SSSZZ", ZoneOffset.UTC, Locale.ROOT); ZonedDateTime datetime = javaFunction.apply("2018-02-05T13:44:56.657+0100"); @@ -52,6 +52,14 @@ public void testParseJavaWithTimeZone() { assertThat(expectedDateTime, is("2018-02-05T12:44:56.657Z")); } + public void testParseYearJavaWithTimeZone() { + Function javaFunction = DateFormat.Java.getFunction("uuuu-MM-dd'T'HH:mm:ss.SSSZZ", + ZoneOffset.UTC, Locale.ROOT); + ZonedDateTime datetime = javaFunction.apply("2018-02-05T13:44:56.657+0100"); + String expectedDateTime = DateFormatter.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").withZone(ZoneOffset.UTC).format(datetime); + assertThat(expectedDateTime, is("2018-02-05T12:44:56.657Z")); + } + public void testParseJavaDefaultYear() { String format = randomFrom("8dd/MM", "dd/MM"); ZoneId timezone = DateUtils.of("Europe/Amsterdam"); From c199e716724bb08ae2cdac22d93d3d0fda82b7c7 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Wed, 18 Sep 2019 09:36:42 +0200 Subject: [PATCH 13/19] support yyyy pattern without day or month --- .../java/org/elasticsearch/common/time/DateFormatters.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 0b1dd3d33c16c..8fd95ae6ca8d0 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java @@ -1858,7 +1858,8 @@ public static ZonedDateTime from(TemporalAccessor accessor) { if (accessor.isSupported(MONTH_OF_YEAR)) { return getFirstOfMonth(accessor).atStartOfDay(zoneId); } else { - return Year.of(accessor.get(ChronoField.YEAR)).atDay(1).atStartOfDay(zoneId); + int year = getYear(accessor); + return Year.of(year).atDay(1).atStartOfDay(zoneId); } } else if (accessor.isSupported(MONTH_OF_YEAR)) { // missing year, falling back to the epoch and then filling From 9ac87b913dd62132af4b2e33c4bb2ae6de198441 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Wed, 18 Sep 2019 11:01:15 +0200 Subject: [PATCH 14/19] change yyyy format to uuu --- .../aggregations/pipeline/DateDerivativeIT.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/DateDerivativeIT.java b/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/DateDerivativeIT.java index 395d7498732c3..8de113b3239a8 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/DateDerivativeIT.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/DateDerivativeIT.java @@ -198,7 +198,7 @@ public void testSingleValuedFieldNormalised_timeZone_CET_DstStart() throws Excep List builders = new ArrayList<>(); ZoneId timezone = ZoneId.of("CET"); - DateFormatter formatter = DateFormatter.forPattern("yyyy-MM-dd'T'HH:mm:ss").withZone(timezone); + DateFormatter formatter = DateFormatter.forPattern("uuuu-MM-dd'T'HH:mm:ss").withZone(timezone); // epoch millis: 1332547200000 addNTimes(1, IDX_DST_START, DateFormatters.from(formatter.parse("2012-03-24T01:00:00")), builders); // day with dst shift, only 23h long @@ -223,7 +223,7 @@ public void testSingleValuedFieldNormalised_timeZone_CET_DstStart() throws Excep List buckets = deriv.getBuckets(); assertThat(buckets.size(), equalTo(4)); - DateFormatter dateFormatter = DateFormatter.forPattern("yyyy-MM-dd"); + DateFormatter dateFormatter = DateFormatter.forPattern("uuuu-MM-dd"); ZonedDateTime expectedKeyFirstBucket = LocalDate.from(dateFormatter.parse("2012-03-24")).atStartOfDay(timezone).withZoneSameInstant(ZoneOffset.UTC); assertBucket(buckets.get(0), expectedKeyFirstBucket, 1L, nullValue(), null, null); @@ -250,7 +250,7 @@ public void testSingleValuedFieldNormalised_timeZone_CET_DstEnd() throws Excepti ZoneId timezone = ZoneId.of("CET"); List builders = new ArrayList<>(); - DateFormatter formatter = DateFormatter.forPattern("yyyy-MM-dd'T'HH:mm:ss").withZone(timezone); + DateFormatter formatter = DateFormatter.forPattern("uuuu-MM-dd'T'HH:mm:ss").withZone(timezone); addNTimes(1, IDX_DST_END, DateFormatters.from(formatter.parse("2012-10-27T01:00:00")), builders); // day with dst shift -1h, 25h long addNTimes(2, IDX_DST_END, DateFormatters.from(formatter.parse("2012-10-28T01:00:00")), builders); @@ -274,7 +274,7 @@ public void testSingleValuedFieldNormalised_timeZone_CET_DstEnd() throws Excepti List buckets = deriv.getBuckets(); assertThat(buckets.size(), equalTo(4)); - DateFormatter dateFormatter = DateFormatter.forPattern("yyyy-MM-dd").withZone(ZoneOffset.UTC); + DateFormatter dateFormatter = DateFormatter.forPattern("uuuu-MM-dd").withZone(ZoneOffset.UTC); ZonedDateTime expectedKeyFirstBucket = LocalDate.from(dateFormatter.parse("2012-10-27")).atStartOfDay(timezone).withZoneSameInstant(ZoneOffset.UTC); @@ -303,7 +303,7 @@ public void testSingleValuedFieldNormalised_timeZone_AsiaKathmandu() throws Exce ZoneId timezone = ZoneId.of("Asia/Kathmandu"); List builders = new ArrayList<>(); - DateFormatter formatter = DateFormatter.forPattern("yyyy-MM-dd'T'HH:mm:ss").withZone(timezone); + DateFormatter formatter = DateFormatter.forPattern("uuuu-MM-dd'T'HH:mm:ss").withZone(timezone); addNTimes(1, IDX_DST_KATHMANDU, DateFormatters.from(formatter.parse("1985-12-31T22:30:00")), builders); // the shift happens during the next bucket, which includes the 45min that do not start on the full hour addNTimes(2, IDX_DST_KATHMANDU, DateFormatters.from(formatter.parse("1985-12-31T23:30:00")), builders); @@ -327,7 +327,7 @@ public void testSingleValuedFieldNormalised_timeZone_AsiaKathmandu() throws Exce List buckets = deriv.getBuckets(); assertThat(buckets.size(), equalTo(4)); - DateFormatter dateFormatter = DateFormatter.forPattern("yyyy-MM-dd'T'HH:mm:ss").withZone(ZoneOffset.UTC); + DateFormatter dateFormatter = DateFormatter.forPattern("uuuu-MM-dd'T'HH:mm:ss").withZone(ZoneOffset.UTC); ZonedDateTime expectedKeyFirstBucket = LocalDateTime.from(dateFormatter.parse("1985-12-31T22:00:00")).atZone(timezone).withZoneSameInstant(ZoneOffset.UTC); From 643760b3872b25a81aa252b01010dc4c7f6596d3 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Fri, 27 Sep 2019 09:09:15 +0200 Subject: [PATCH 15/19] field rename and method ref rem --- .../common/time/JavaDateMathParser.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 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..9e915c05dfc21 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; /** @@ -48,11 +47,11 @@ public class JavaDateMathParser implements DateMathParser { private final JavaDateFormatter formatter; private final String format; - private JavaDateFormatter roundupParser2; + private JavaDateFormatter roundUpFormatter; - JavaDateMathParser(String format, JavaDateFormatter formatter, JavaDateFormatter roundupParser2) { + JavaDateMathParser(String format, JavaDateFormatter formatter, JavaDateFormatter roundUpFormatter) { this.format = format; - this.roundupParser2 = roundupParser2; + this.roundUpFormatter = roundUpFormatter; Objects.requireNonNull(formatter); this.formatter = formatter; } @@ -216,12 +215,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; + JavaDateFormatter formatter = roundUpIfNoTime ? this.roundUpFormatter : 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 acb056466a99d3cb833d796ea0e613955050802e Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Mon, 30 Sep 2019 10:40:26 +0200 Subject: [PATCH 16/19] fix test to use uuuu instead yyyy --- .../java/org/elasticsearch/common/time/DateFormatters.java | 3 ++- .../java/org/elasticsearch/common/time/JavaDateFormatter.java | 1 + .../xpack/core/ilm/IndexLifecycleOriginationDateParser.java | 2 +- .../core/ilm/IndexLifecycleOriginationDateParserTests.java | 2 +- 4 files changed, 5 insertions(+), 3 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 ba9e8ee9abae9..0bdf318ce78b4 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java @@ -1618,7 +1618,8 @@ public class DateFormatters { // ///////////////////////////////////////// - static DateFormatter forPattern(String input) { + static DateFormatter + forPattern(String input) { if (Strings.hasLength(input)) { input = input.trim(); } 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 e1ac6db119609..1befdb978cabc 100644 --- a/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java @@ -22,6 +22,7 @@ import org.elasticsearch.common.Strings; import java.text.ParsePosition; +import java.time.DateTimeException; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleOriginationDateParser.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleOriginationDateParser.java index 05b362d733e9c..795ddcbc62e91 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleOriginationDateParser.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleOriginationDateParser.java @@ -17,7 +17,7 @@ public class IndexLifecycleOriginationDateParser { - private static final DateFormatter DATE_FORMATTER = DateFormatter.forPattern("yyyy.MM.dd"); + private static final DateFormatter DATE_FORMATTER = DateFormatter.forPattern("uuuu.MM.dd"); private static final String INDEX_NAME_REGEX = "^.*-(\\d{4}.\\d{2}.\\d{2})(-[\\d]+)?$"; private static final Pattern INDEX_NAME_PATTERN = Pattern.compile(INDEX_NAME_REGEX); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleOriginationDateParserTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleOriginationDateParserTests.java index 520a316aaee4c..9775e0aa280e7 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleOriginationDateParserTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleOriginationDateParserTests.java @@ -17,7 +17,7 @@ public class IndexLifecycleOriginationDateParserTests extends ESTestCase { - private static final DateFormatter dateFormatter = DateFormatter.forPattern("yyyy.MM.dd"); + private static final DateFormatter dateFormatter = DateFormatter.forPattern("uuuu.MM.dd"); public void testShouldParseIndexNameReturnsFalseWhenOriginationDateIsSet() { Settings settings = Settings.builder() From 050ec421d7d788d9001522938d813585b96e7437 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Mon, 30 Sep 2019 11:33:19 +0200 Subject: [PATCH 17/19] checkstyle --- .../java/org/elasticsearch/common/time/JavaDateFormatter.java | 1 - 1 file changed, 1 deletion(-) 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 1befdb978cabc..e1ac6db119609 100644 --- a/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java @@ -22,7 +22,6 @@ import org.elasticsearch.common.Strings; import java.text.ParsePosition; -import java.time.DateTimeException; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; From 4ebd7b58540fdbf514c89ac05dda9ce1d74002fc Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Mon, 30 Sep 2019 14:50:26 +0200 Subject: [PATCH 18/19] disable year-of-era relaxation to fail tests --- .../org/elasticsearch/ingest/common/DateFormat.java | 2 +- .../elasticsearch/common/time/DateFormatters.java | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java index c4b96bb06b0b2..7da69ed1b2bea 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java @@ -100,7 +100,7 @@ Function getFunction(String format, ZoneId zoneId, Locale TemporalAccessor accessor = formatter.parse(text); // if there is no year nor year-of-era, we fall back to the current one and // fill the rest of the date up with the parsed date - if (accessor.isSupported(ChronoField.YEAR) == false && accessor.isSupported(ChronoField.YEAR_OF_ERA) == false ) { + if (accessor.isSupported(ChronoField.YEAR) == false /*&& accessor.isSupported(ChronoField.YEAR_OF_ERA) == false */) { int year = LocalDate.now(ZoneOffset.UTC).getYear(); ZonedDateTime newTime = Instant.EPOCH.atZone(ZoneOffset.UTC).withYear(year); for (ChronoField field : FIELDS) { 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 0bdf318ce78b4..eab748c6acee4 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java @@ -1841,7 +1841,7 @@ public static ZonedDateTime from(TemporalAccessor accessor) { zoneId = ZoneOffset.UTC; } - LocalDate localDate = accessor.query(LOCAL_DATE_QUERY); + LocalDate localDate = accessor.query(TemporalQueries.localDate());//accessor.query(LOCAL_DATE_QUERY); LocalTime localTime = accessor.query(TemporalQueries.localTime()); boolean isLocalDateSet = localDate != null; boolean isLocalTimeSet = localTime != null; @@ -1859,8 +1859,10 @@ public static ZonedDateTime from(TemporalAccessor accessor) { if (accessor.isSupported(MONTH_OF_YEAR)) { return getFirstOfMonth(accessor).atStartOfDay(zoneId); } else { - int year = getYear(accessor); - return Year.of(year).atDay(1).atStartOfDay(zoneId); + return Year.of(accessor.get(ChronoField.YEAR)).atDay(1).atStartOfDay(zoneId); + +// int year = getYear(accessor); +// return Year.of(year).atDay(1).atStartOfDay(zoneId); } } else if (accessor.isSupported(MONTH_OF_YEAR)) { // missing year, falling back to the epoch and then filling @@ -1941,6 +1943,8 @@ private static ZonedDateTime of(LocalDate localDate, LocalTime localTime, ZoneId @SuppressForbidden(reason = "LocalDate.of is fine here") private static LocalDate getFirstOfMonth(TemporalAccessor accessor) { - return LocalDate.of(getYear(accessor), accessor.get(MONTH_OF_YEAR), 1); +// return LocalDate.of(getYear(accessor), accessor.get(MONTH_OF_YEAR), 1); + return LocalDate.of(accessor.get(ChronoField.YEAR), accessor.get(MONTH_OF_YEAR), 1); + } } From fe0404a63a881aff945f1854734de17d4fbdfc11 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Mon, 30 Sep 2019 15:46:45 +0200 Subject: [PATCH 19/19] Revert "disable year-of-era relaxation to fail tests" This reverts commit 4ebd7b58540fdbf514c89ac05dda9ce1d74002fc. --- .../org/elasticsearch/ingest/common/DateFormat.java | 2 +- .../elasticsearch/common/time/DateFormatters.java | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java index 7da69ed1b2bea..c4b96bb06b0b2 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java @@ -100,7 +100,7 @@ Function getFunction(String format, ZoneId zoneId, Locale TemporalAccessor accessor = formatter.parse(text); // if there is no year nor year-of-era, we fall back to the current one and // fill the rest of the date up with the parsed date - if (accessor.isSupported(ChronoField.YEAR) == false /*&& accessor.isSupported(ChronoField.YEAR_OF_ERA) == false */) { + if (accessor.isSupported(ChronoField.YEAR) == false && accessor.isSupported(ChronoField.YEAR_OF_ERA) == false ) { int year = LocalDate.now(ZoneOffset.UTC).getYear(); ZonedDateTime newTime = Instant.EPOCH.atZone(ZoneOffset.UTC).withYear(year); for (ChronoField field : FIELDS) { 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 eab748c6acee4..0bdf318ce78b4 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java @@ -1841,7 +1841,7 @@ public static ZonedDateTime from(TemporalAccessor accessor) { zoneId = ZoneOffset.UTC; } - LocalDate localDate = accessor.query(TemporalQueries.localDate());//accessor.query(LOCAL_DATE_QUERY); + LocalDate localDate = accessor.query(LOCAL_DATE_QUERY); LocalTime localTime = accessor.query(TemporalQueries.localTime()); boolean isLocalDateSet = localDate != null; boolean isLocalTimeSet = localTime != null; @@ -1859,10 +1859,8 @@ public static ZonedDateTime from(TemporalAccessor accessor) { if (accessor.isSupported(MONTH_OF_YEAR)) { return getFirstOfMonth(accessor).atStartOfDay(zoneId); } else { - return Year.of(accessor.get(ChronoField.YEAR)).atDay(1).atStartOfDay(zoneId); - -// int year = getYear(accessor); -// return Year.of(year).atDay(1).atStartOfDay(zoneId); + int year = getYear(accessor); + return Year.of(year).atDay(1).atStartOfDay(zoneId); } } else if (accessor.isSupported(MONTH_OF_YEAR)) { // missing year, falling back to the epoch and then filling @@ -1943,8 +1941,6 @@ private static ZonedDateTime of(LocalDate localDate, LocalTime localTime, ZoneId @SuppressForbidden(reason = "LocalDate.of is fine here") private static LocalDate getFirstOfMonth(TemporalAccessor accessor) { -// return LocalDate.of(getYear(accessor), accessor.get(MONTH_OF_YEAR), 1); - return LocalDate.of(accessor.get(ChronoField.YEAR), accessor.get(MONTH_OF_YEAR), 1); - + return LocalDate.of(getYear(accessor), accessor.get(MONTH_OF_YEAR), 1); } }