1
0
Эх сурвалжийг харах

Fix ingest java week based year defaulting (#65717)

If year, year of era, or weekbased year is not specified ingest Java
date processor is defaulting year to current year.
However the current implementation has mistaken weekBasedYear field with
weekOfWeekBasedYear. This has lead to incorrect defaulting.

relates #63458
Przemyslaw Gomulka 4 жил өмнө
parent
commit
8f74f18257

+ 1 - 1
modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java

@@ -109,7 +109,7 @@ enum DateFormat {
                 // fill the rest of the date up with the parsed date
                 if (accessor.isSupported(ChronoField.YEAR) == false
                     && accessor.isSupported(ChronoField.YEAR_OF_ERA) == false
-                    && accessor.isSupported(WeekFields.of(locale).weekOfWeekBasedYear()) == false) {
+                    && accessor.isSupported(WeekFields.of(locale).weekBasedYear()) == false) {
                     int year = LocalDate.now(ZoneOffset.UTC).getYear();
                     ZonedDateTime newTime = Instant.EPOCH.atZone(ZoneOffset.UTC).withYear(year);
                     for (ChronoField field : FIELDS) {

+ 10 - 2
modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateFormatTests.java

@@ -69,14 +69,22 @@ public class DateFormatTests extends ESTestCase {
         assertThat(dateTime.getYear(), is(year));
     }
 
-    public void testParseWeekBased() {
-        String format = randomFrom("YYYY-ww");
+    public void testParseWeekBasedYearAndWeek() {
+        String format = "YYYY-ww";
         ZoneId timezone = DateUtils.of("Europe/Amsterdam");
         Function<String, ZonedDateTime> javaFunction = DateFormat.Java.getFunction(format, timezone, Locale.ROOT);
         ZonedDateTime dateTime = javaFunction.apply("2020-33");
         assertThat(dateTime, equalTo(ZonedDateTime.of(2020,8,10,0,0,0,0,timezone)));
     }
 
+    public void testParseWeekBasedYear() {
+        String format = "YYYY";
+        ZoneId timezone = DateUtils.of("Europe/Amsterdam");
+        Function<String, ZonedDateTime> javaFunction = DateFormat.Java.getFunction(format, timezone, Locale.ROOT);
+        ZonedDateTime dateTime = javaFunction.apply("2019");
+        assertThat(dateTime, equalTo(ZonedDateTime.of(2018,12,31,0,0,0,0,timezone)));
+    }
+
     public void testParseWeekBasedWithLocale() {
         String format = randomFrom("YYYY-ww");
         ZoneId timezone = DateUtils.of("Europe/Amsterdam");