Skip to content

Commit

Permalink
Fix DateFormatters.parseMillis when no timezone is given
Browse files Browse the repository at this point in the history
The parseMillis method was able to work on formats without timezones by
falling back to UTC. The Java Date Formatter 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 elastic#39067
  • Loading branch information
spinscale committed Feb 19, 2019
1 parent f3e8d66 commit 39bf475
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public interface DateFormatter {
* Parse the given input into millis-since-epoch.
*/
default long parseMillis(String input) {
return Instant.from(parse(input)).toEpochMilli();
return DateFormatters.from(parse(input)).toInstant().toEpochMilli();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,12 @@ public void testIso8601Parsers() {
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) {
assertThat(jodaDate.getMillis(), is(javaDate.toInstant().toEpochMilli()));
String javaTimeOut = DateFormatter.forPattern(format).format(javaDate);
Expand Down

0 comments on commit 39bf475

Please sign in to comment.