Skip to content

Commit a5c05af

Browse files
authored
Explicitly use ISO weekfields for built-in weekyear date formats (elastic#113787)
This is so it doesn't change when changing JDK version and locale database
1 parent 45a08b9 commit a5c05af

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Function<String, ZonedDateTime> getFunction(String format, ZoneId zoneId, Locale
9797
// fill the rest of the date up with the parsed date
9898
if (accessor.isSupported(ChronoField.YEAR) == false
9999
&& accessor.isSupported(ChronoField.YEAR_OF_ERA) == false
100+
&& accessor.isSupported(WeekFields.ISO.weekBasedYear()) == false
100101
&& accessor.isSupported(WeekFields.of(locale).weekBasedYear()) == false
101102
&& accessor.isSupported(ChronoField.INSTANT_SECONDS) == false) {
102103
int year = LocalDate.now(ZoneOffset.UTC).getYear();

server/src/main/java/org/elasticsearch/common/time/DateFormatters.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private static DateFormatter newDateFormatter(String format, DateTimeFormatter p
8282
);
8383
}
8484

85-
public static final WeekFields WEEK_FIELDS_ROOT = WeekFields.of(Locale.ROOT);
85+
public static final WeekFields WEEK_FIELDS_ROOT = WeekFields.ISO;
8686

8787
private static final DateTimeFormatter TIME_ZONE_FORMATTER_NO_COLON = new DateTimeFormatterBuilder().appendOffset("+HHmm", "Z")
8888
.toFormatter(Locale.ROOT)
@@ -2391,6 +2391,7 @@ public static ZonedDateTime from(TemporalAccessor accessor, Locale locale, ZoneI
23912391
boolean isLocalTimeSet = localTime != null;
23922392

23932393
// the first two cases are the most common, so this allows us to exit early when parsing dates
2394+
WeekFields localeWeekFields;
23942395
if (isLocalDateSet && isLocalTimeSet) {
23952396
return of(localDate, localTime, zoneId);
23962397
} else if (accessor.isSupported(ChronoField.INSTANT_SECONDS) && accessor.isSupported(NANO_OF_SECOND)) {
@@ -2409,17 +2410,18 @@ public static ZonedDateTime from(TemporalAccessor accessor, Locale locale, ZoneI
24092410
} else if (accessor.isSupported(MONTH_OF_YEAR)) {
24102411
// missing year, falling back to the epoch and then filling
24112412
return getLocalDate(accessor, locale).atStartOfDay(zoneId);
2412-
} else if (accessor.isSupported(WeekFields.of(locale).weekBasedYear())) {
2413-
return localDateFromWeekBasedDate(accessor, locale).atStartOfDay(zoneId);
2413+
} else if (accessor.isSupported(WeekFields.ISO.weekBasedYear())) {
2414+
return localDateFromWeekBasedDate(accessor, locale, WeekFields.ISO).atStartOfDay(zoneId);
2415+
} else if (accessor.isSupported((localeWeekFields = WeekFields.of(locale)).weekBasedYear())) {
2416+
return localDateFromWeekBasedDate(accessor, locale, localeWeekFields).atStartOfDay(zoneId);
24142417
}
24152418

24162419
// we should not reach this piece of code, everything being parsed we should be able to
24172420
// convert to a zoned date time! If not, we have to extend the above methods
24182421
throw new IllegalArgumentException("temporal accessor [" + accessor + "] cannot be converted to zoned date time");
24192422
}
24202423

2421-
private static LocalDate localDateFromWeekBasedDate(TemporalAccessor accessor, Locale locale) {
2422-
WeekFields weekFields = WeekFields.of(locale);
2424+
private static LocalDate localDateFromWeekBasedDate(TemporalAccessor accessor, Locale locale, WeekFields weekFields) {
24232425
if (accessor.isSupported(weekFields.weekOfWeekBasedYear())) {
24242426
return LocalDate.ofEpochDay(0)
24252427
.with(weekFields.weekBasedYear(), accessor.get(weekFields.weekBasedYear()))
@@ -2461,8 +2463,11 @@ public String toString() {
24612463
};
24622464

24632465
private static LocalDate getLocalDate(TemporalAccessor accessor, Locale locale) {
2464-
if (accessor.isSupported(WeekFields.of(locale).weekBasedYear())) {
2465-
return localDateFromWeekBasedDate(accessor, locale);
2466+
WeekFields localeWeekFields;
2467+
if (accessor.isSupported(WeekFields.ISO.weekBasedYear())) {
2468+
return localDateFromWeekBasedDate(accessor, locale, WeekFields.ISO);
2469+
} else if (accessor.isSupported((localeWeekFields = WeekFields.of(locale)).weekBasedYear())) {
2470+
return localDateFromWeekBasedDate(accessor, locale, localeWeekFields);
24662471
} else if (accessor.isSupported(MONTH_OF_YEAR)) {
24672472
int year = getYear(accessor);
24682473
if (accessor.isSupported(DAY_OF_MONTH)) {

0 commit comments

Comments
 (0)