Fix DateFormatters.parseMillis when no timezone is given (#39100)

The parseMillis method was able to work on formats without timezones by
falling back to UTC. The Date Formatter interface did not support this, as
the calling code was using the `Instant.from` java time API.

This switches over to an internal method which adds UTC as a timezone.

Closes #39067
This commit is contained in:
Alexander Reelsen 2019-02-19 14:11:52 +01:00
parent 3cd6feeb0b
commit 7f8a640363
2 changed files with 7 additions and 1 deletions

View File

@ -47,7 +47,7 @@ public interface DateFormatter {
* Parse the given input into millis-since-epoch. * Parse the given input into millis-since-epoch.
*/ */
default long parseMillis(String input) { default long parseMillis(String input) {
return Instant.from(parse(input)).toEpochMilli(); return DateFormatters.from(parse(input)).toInstant().toEpochMilli();
} }
/** /**

View File

@ -683,6 +683,12 @@ public class JavaJodaTimeDuellingTests extends ESTestCase {
assertSameDate("2018-10-10T10:11:12,123Z", format, jodaFormatter, javaFormatter); assertSameDate("2018-10-10T10:11:12,123Z", format, jodaFormatter, javaFormatter);
} }
public void testParsingMissingTimezone() {
long millisJava = DateFormatter.forPattern("8yyyy-MM-dd HH:mm:ss").parseMillis("2018-02-18 17:47:17");
long millisJoda = DateFormatter.forPattern("yyyy-MM-dd HH:mm:ss").parseMillis("2018-02-18 17:47:17");
assertThat(millisJava, is(millisJoda));
}
private void assertSamePrinterOutput(String format, ZonedDateTime javaDate, DateTime jodaDate) { private void assertSamePrinterOutput(String format, ZonedDateTime javaDate, DateTime jodaDate) {
assertThat(jodaDate.getMillis(), is(javaDate.toInstant().toEpochMilli())); assertThat(jodaDate.getMillis(), is(javaDate.toInstant().toEpochMilli()));
String javaTimeOut = DateFormatter.forPattern(format).format(javaDate); String javaTimeOut = DateFormatter.forPattern(format).format(javaDate);