Scripting: Properly support no-offset date formatting (#36316)

The conversion of timezones in JodaCompatibleZonedDateTime from joda to
java time requires the use of the DateUtils class to cater for corner
cases.

Closes #36306
This commit is contained in:
Alexander Reelsen 2018-12-07 09:40:58 +01:00 committed by GitHub
parent 7693d538ca
commit 6a987415f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 3 deletions

View File

@ -22,8 +22,8 @@ package org.elasticsearch.script;
import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.time.DateUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.security.AccessController;
import java.security.PrivilegedAction;
@ -391,14 +391,14 @@ public class JodaCompatibleZonedDateTime {
public String toString(String format) {
logDeprecatedMethod("toString(String)", "a DateTimeFormatter");
// TODO: replace with bwc formatter
return new DateTime(dt.toInstant().toEpochMilli(), DateTimeZone.forID(dt.getZone().getId())).toString(format);
return new DateTime(dt.toInstant().toEpochMilli(), DateUtils.zoneIdToDateTimeZone(dt.getZone())).toString(format);
}
@Deprecated
public String toString(String format, Locale locale) {
logDeprecatedMethod("toString(String,Locale)", "a DateTimeFormatter");
// TODO: replace with bwc formatter
return new DateTime(dt.toInstant().toEpochMilli(), DateTimeZone.forID(dt.getZone().getId())).toString(format, locale);
return new DateTime(dt.toInstant().toEpochMilli(), DateUtils.zoneIdToDateTimeZone(dt.getZone())).toString(format, locale);
}
public DayOfWeek getDayOfWeekEnum() {

View File

@ -237,4 +237,14 @@ public class JodaCompatibleZonedDateTimeTests extends ESTestCase {
public void testDayOfWeekEnum() {
assertThat(javaTime.getDayOfWeekEnum(), equalTo(DayOfWeek.of(jodaTime.getDayOfWeek())));
}
public void testToStringWithLocaleAndZeroOffset() {
JodaCompatibleZonedDateTime dt = new JodaCompatibleZonedDateTime(Instant.EPOCH, ZoneOffset.ofTotalSeconds(0));
assertMethodDeprecation(() -> dt.toString("yyyy-MM-dd hh:mm", Locale.ROOT), "toString(String,Locale)", "a DateTimeFormatter");
}
public void testToStringAndZeroOffset() {
JodaCompatibleZonedDateTime dt = new JodaCompatibleZonedDateTime(Instant.EPOCH, ZoneOffset.ofTotalSeconds(0));
assertMethodDeprecation(() -> dt.toString("yyyy-MM-dd hh:mm"), "toString(String)", "a DateTimeFormatter");
}
}