Skip to content

Commit 35ed137

Browse files
authored
Ensure joda compatibility in custom date formats (#38171)
If custom date formats are used, there may be combinations that the new performat DateFormatters.from() method has not covered yet. This adds a few such corner cases and ensures the tests are correctly commented out.
1 parent 66e4fb4 commit 35ed137

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1604,13 +1604,16 @@ public static ZonedDateTime from(TemporalAccessor accessor) {
16041604
} else if (isLocalDateSet) {
16051605
return localDate.atStartOfDay(zoneId);
16061606
} else if (isLocalTimeSet) {
1607-
return of(LOCALDATE_EPOCH, localTime, zoneId);
1607+
return of(getLocaldate(accessor), localTime, zoneId);
16081608
} else if (accessor.isSupported(ChronoField.YEAR)) {
16091609
if (accessor.isSupported(MONTH_OF_YEAR)) {
16101610
return getFirstOfMonth(accessor).atStartOfDay(zoneId);
16111611
} else {
16121612
return Year.of(accessor.get(ChronoField.YEAR)).atDay(1).atStartOfDay(zoneId);
16131613
}
1614+
} else if (accessor.isSupported(MONTH_OF_YEAR)) {
1615+
// missing year, falling back to the epoch and then filling
1616+
return getLocaldate(accessor).atStartOfDay(zoneId);
16141617
} else if (accessor.isSupported(WeekFields.ISO.weekBasedYear())) {
16151618
if (accessor.isSupported(WeekFields.ISO.weekOfWeekBasedYear())) {
16161619
return Year.of(accessor.get(WeekFields.ISO.weekBasedYear()))
@@ -1630,6 +1633,18 @@ public static ZonedDateTime from(TemporalAccessor accessor) {
16301633
throw new IllegalArgumentException("temporal accessor [" + accessor + "] cannot be converted to zoned date time");
16311634
}
16321635

1636+
private static LocalDate getLocaldate(TemporalAccessor accessor) {
1637+
if (accessor.isSupported(MONTH_OF_YEAR)) {
1638+
if (accessor.isSupported(DAY_OF_MONTH)) {
1639+
return LocalDate.of(1970, accessor.get(MONTH_OF_YEAR), accessor.get(DAY_OF_MONTH));
1640+
} else {
1641+
return LocalDate.of(1970, accessor.get(MONTH_OF_YEAR), 1);
1642+
}
1643+
}
1644+
1645+
return LOCALDATE_EPOCH;
1646+
}
1647+
16331648
@SuppressForbidden(reason = "ZonedDateTime.of is fine here")
16341649
private static ZonedDateTime of(LocalDate localDate, LocalTime localTime, ZoneId zoneId) {
16351650
return ZonedDateTime.of(localDate, localTime, zoneId);

server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,14 @@ public void testTimeZoneFormatting() {
6262
formatter3.parse("20181126T121212.123-0830");
6363
}
6464

65+
public void testCustomTimeFormats() {
66+
assertSameDate("2010 12 06 11:05:15", "yyyy dd MM HH:mm:ss");
67+
assertSameDate("12/06", "dd/MM");
68+
assertSameDate("Nov 24 01:29:01 -0800", "MMM dd HH:mm:ss Z");
69+
}
70+
6571
// this test requires tests to run with -Djava.locale.providers=COMPAT in order to work
66-
// public void testCustomTimeFormats() {
67-
// assertSameDate("2010 12 06 11:05:15", "yyyy dd MM HH:mm:ss");
68-
// assertSameDate("12/06", "dd/MM");
69-
// assertSameDate("Nov 24 01:29:01 -0800", "MMM dd HH:mm:ss Z");
72+
// public void testCustomLocales() {
7073
//
7174
// // also ensure that locale based dates are the same
7275
// assertSameDate("Di., 05 Dez. 2000 02:55:00 -0800", "E, d MMM yyyy HH:mm:ss Z", LocaleUtils.parse("de"));

0 commit comments

Comments
 (0)