Skip to content

Commit 8f74f18

Browse files
authored
Fix ingest java week based year defaulting (elastic#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 elastic#63458
1 parent 820a813 commit 8f74f18

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Function<String, ZonedDateTime> getFunction(String format, ZoneId zoneId, Locale
109109
// fill the rest of the date up with the parsed date
110110
if (accessor.isSupported(ChronoField.YEAR) == false
111111
&& accessor.isSupported(ChronoField.YEAR_OF_ERA) == false
112-
&& accessor.isSupported(WeekFields.of(locale).weekOfWeekBasedYear()) == false) {
112+
&& accessor.isSupported(WeekFields.of(locale).weekBasedYear()) == false) {
113113
int year = LocalDate.now(ZoneOffset.UTC).getYear();
114114
ZonedDateTime newTime = Instant.EPOCH.atZone(ZoneOffset.UTC).withYear(year);
115115
for (ChronoField field : FIELDS) {

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,22 @@ public void testParseJavaDefaultYear() {
6969
assertThat(dateTime.getYear(), is(year));
7070
}
7171

72-
public void testParseWeekBased() {
73-
String format = randomFrom("YYYY-ww");
72+
public void testParseWeekBasedYearAndWeek() {
73+
String format = "YYYY-ww";
7474
ZoneId timezone = DateUtils.of("Europe/Amsterdam");
7575
Function<String, ZonedDateTime> javaFunction = DateFormat.Java.getFunction(format, timezone, Locale.ROOT);
7676
ZonedDateTime dateTime = javaFunction.apply("2020-33");
7777
assertThat(dateTime, equalTo(ZonedDateTime.of(2020,8,10,0,0,0,0,timezone)));
7878
}
7979

80+
public void testParseWeekBasedYear() {
81+
String format = "YYYY";
82+
ZoneId timezone = DateUtils.of("Europe/Amsterdam");
83+
Function<String, ZonedDateTime> javaFunction = DateFormat.Java.getFunction(format, timezone, Locale.ROOT);
84+
ZonedDateTime dateTime = javaFunction.apply("2019");
85+
assertThat(dateTime, equalTo(ZonedDateTime.of(2018,12,31,0,0,0,0,timezone)));
86+
}
87+
8088
public void testParseWeekBasedWithLocale() {
8189
String format = randomFrom("YYYY-ww");
8290
ZoneId timezone = DateUtils.of("Europe/Amsterdam");

0 commit comments

Comments
 (0)