Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DateFormatters.parseMillis when no timezone is given #39100

Merged
merged 1 commit into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix DateFormatters.parseMillis when no timezone is given
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 #39067
  • Loading branch information
spinscale committed Feb 19, 2019
commit 39bf47574d714bd8b76fb7314bc4cb4d30eb0935
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