diff --git a/core/src/main/java/org/elasticsearch/common/joda/Joda.java b/core/src/main/java/org/elasticsearch/common/joda/Joda.java index 34c882d0d8096..2da0e7d2b6865 100644 --- a/core/src/main/java/org/elasticsearch/common/joda/Joda.java +++ b/core/src/main/java/org/elasticsearch/common/joda/Joda.java @@ -336,9 +336,10 @@ public int parseInto(DateTimeParserBucket bucket, String text, int position) { boolean isPositive = text.startsWith("-") == false; boolean isTooLong = text.length() > estimateParsedLength(); - if ((isPositive && isTooLong) || - // timestamps have to have UTC timezone - bucket.getZone() != DateTimeZone.UTC) { + if (bucket.getZone() != DateTimeZone.UTC) { + String format = hasMilliSecondPrecision ? "epoch_millis" : "epoch_second"; + throw new IllegalArgumentException("time_zone must be UTC for format [" + format + "]"); + } else if (isPositive && isTooLong) { return -1; } diff --git a/core/src/test/java/org/elasticsearch/deps/joda/SimpleJodaTests.java b/core/src/test/java/org/elasticsearch/deps/joda/SimpleJodaTests.java index 442a566a77e8b..91c9814b45fc8 100644 --- a/core/src/test/java/org/elasticsearch/deps/joda/SimpleJodaTests.java +++ b/core/src/test/java/org/elasticsearch/deps/joda/SimpleJodaTests.java @@ -317,6 +317,36 @@ public void testForInvalidDatesInEpochMillis() { } } + public void testForInvalidTimeZoneWithEpochSeconds() { + DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder() + .append(new Joda.EpochTimeParser(false)) + .toFormatter() + .withZone(DateTimeZone.forOffsetHours(1)); + FormatDateTimeFormatter formatter = + new FormatDateTimeFormatter("epoch_seconds", dateTimeFormatter, Locale.ROOT); + try { + formatter.parser().parseDateTime("1433144433655"); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage(), containsString("time_zone must be UTC")); + } + } + + public void testForInvalidTimeZoneWithEpochMillis() { + DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder() + .append(new Joda.EpochTimeParser(true)) + .toFormatter() + .withZone(DateTimeZone.forOffsetHours(1)); + FormatDateTimeFormatter formatter = + new FormatDateTimeFormatter("epoch_millis", dateTimeFormatter, Locale.ROOT); + try { + formatter.parser().parseDateTime("1433144433"); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage(), containsString("time_zone must be UTC")); + } + } + public void testThatEpochParserIsPrinter() { FormatDateTimeFormatter formatter = Joda.forPattern("epoch_millis"); assertThat(formatter.parser().isPrinter(), is(true));