From 9c10ce904d5cb8b9b93fd4f404e858fb6b2c81d9 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Sun, 16 Dec 2018 23:38:53 -0800 Subject: [PATCH 1/2] Core: Deprecate use of scientific notation in epoch time parsing The joda epoch parsing code currently supports passing epoch time as a number in scientific notation. However, no systems appear to exist which output timestamps in scientific notation. In java time, it is particularly complex to implement scientific notation timestamp parsing within a DateTimeFormatter. This commit adds a deprecation warning when the epoch time parsers in joda parse scientific notation, so that it can be removed when switching to java time. joda are passed a time in scientific notation. --- .../main/java/org/elasticsearch/common/joda/Joda.java | 8 ++++++++ .../org/elasticsearch/common/joda/SimpleJodaTests.java | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/server/src/main/java/org/elasticsearch/common/joda/Joda.java b/server/src/main/java/org/elasticsearch/common/joda/Joda.java index f999811645518..265cc2edce14c 100644 --- a/server/src/main/java/org/elasticsearch/common/joda/Joda.java +++ b/server/src/main/java/org/elasticsearch/common/joda/Joda.java @@ -47,6 +47,7 @@ import java.io.Writer; import java.math.BigDecimal; import java.util.Locale; +import java.util.regex.Pattern; public class Joda { @@ -321,6 +322,8 @@ public DateTimeField getField(Chronology chronology) { public static class EpochTimeParser implements DateTimeParser { + private static final Pattern scientificNotation = Pattern.compile("[Ee]"); + private final boolean hasMilliSecondPrecision; public EpochTimeParser(boolean hasMilliSecondPrecision) { @@ -348,6 +351,11 @@ public int parseInto(DateTimeParserBucket bucket, String text, int position) { int factor = hasMilliSecondPrecision ? 1 : 1000; try { long millis = new BigDecimal(text).longValue() * factor; + // check for deprecation, but after it has parsed correctly so the "e" isn't from something else + if (scientificNotation.matcher(text).find()) { + deprecationLogger.deprecatedAndMaybeLog("epoch-scientific-notation", "Use of scientific notation" + + "in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch."); + } DateTime dt = new DateTime(millis, DateTimeZone.UTC); bucket.saveField(DateTimeFieldType.year(), dt.getYear()); bucket.saveField(DateTimeFieldType.monthOfYear(), dt.getMonthOfYear()); diff --git a/server/src/test/java/org/elasticsearch/common/joda/SimpleJodaTests.java b/server/src/test/java/org/elasticsearch/common/joda/SimpleJodaTests.java index 5f0bff1abf514..2da3a08629f52 100644 --- a/server/src/test/java/org/elasticsearch/common/joda/SimpleJodaTests.java +++ b/server/src/test/java/org/elasticsearch/common/joda/SimpleJodaTests.java @@ -753,6 +753,15 @@ public void testDeprecatedFormatSpecifiers() { " next major version of Elasticsearch. Prefix your date format with '8' to use the new specifier."); } + public void testDeprecatedScientificNotation() { + assertValidDateFormatParsing("epoch_second", "1.234e5", "123400"); + assertWarnings("Use of scientific notation" + + "in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch."); + assertValidDateFormatParsing("epoch_millis", "1.234e5", "123400"); + assertWarnings("Use of scientific notation" + + "in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch."); + } + private void assertValidDateFormatParsing(String pattern, String dateToParse) { assertValidDateFormatParsing(pattern, dateToParse, dateToParse); } From ca18b7e804714275a4ea83c95d74a300e4f6dfe7 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 17 Dec 2018 08:25:06 -0800 Subject: [PATCH 2/2] remove scientific notation from duelling tests --- .../elasticsearch/common/joda/JavaJodaTimeDuellingTests.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java index d32824df419ca..c85d303cf293f 100644 --- a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java +++ b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java @@ -77,13 +77,11 @@ public void testDuellingFormatsValidParsing() { assertSameDate("1", "epoch_second"); assertSameDate("-1", "epoch_second"); assertSameDate("-1522332219", "epoch_second"); - assertSameDate("1.0e3", "epoch_second"); assertSameDate("1522332219321", "epoch_millis"); assertSameDate("0", "epoch_millis"); assertSameDate("1", "epoch_millis"); assertSameDate("-1", "epoch_millis"); assertSameDate("-1522332219321", "epoch_millis"); - assertSameDate("1.0e3", "epoch_millis"); assertSameDate("20181126", "basic_date"); assertSameDate("20181126T121212.123Z", "basic_date_time");