Skip to content

Commit

Permalink
fix: better error message if tz invalid in datetime string (#3449)
Browse files Browse the repository at this point in the history
  • Loading branch information
big-andy-coates authored Oct 2, 2019
1 parent c600858 commit e93c445
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public class PartialStringToTimestampParser {

private static final String HELP_MESSAGE = System.lineSeparator()
+ "Required format is: \"" + KsqlConstants.DATE_TIME_PATTERN + "\", "
+ "with an optional numeric timezone. "
+ "with an optional numeric 4-digit timezone, for example: "
+ "'2020-05-26T23.59.58.000' or with tz: '2020-05-26T23.59.58.000+0200'. "
+ "Partials are also supported, for example \"2020-05-26\"";

private static final StringToTimestampParser PARSER =
Expand All @@ -57,11 +58,8 @@ public long parse(final String text) {
}

try {
if (timezone.length() > 0) {
return PARSER.parse(date + "T" + time, ZoneId.of(timezone));
} else {
return PARSER.parse(date + "T" + time);
}
final ZoneId zoneId = parseTimezone(timezone);
return PARSER.parse(date + "T" + time, zoneId);
} catch (final RuntimeException e) {
throw new KsqlException("Failed to parse timestamp '" + text
+ "': " + e.getMessage()
Expand All @@ -83,6 +81,21 @@ private static String getTimezone(final String time) {
return "";
}

private static ZoneId parseTimezone(final String timezone) {
if (timezone.trim().isEmpty()) {
return ZoneId.systemDefault();
}

try {
return ZoneId.of(timezone);
} catch (final Exception e) {
throw new KsqlException("Failed to parse timezone '" + timezone
+ "': " + e.getMessage(),
e
);
}
}

private static String completeDate(final String date) {
final String[] parts = date.split("-");
if (parts.length == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void shouldThrowOnTimezoneParseError() {
public void shouldIncludeRequiredFormatInErrorMessage() {
// Expect:
expectedException.expectMessage("Required format is: \"yyyy-MM-dd'T'HH:mm:ss.SSS\", "
+ "with an optional numeric timezone. Partials are also supported, for example \"2020-05-26\"");
+ "with an optional numeric 4-digit timezone");

// When:
parser.parse("2017-01-01T00:00:00.000+foo");
Expand Down

0 comments on commit e93c445

Please sign in to comment.