Skip to content

Commit

Permalink
Core: Deprecate use of scientific notation in epoch time parsing (#36691
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
rjernst authored Dec 18, 2018
1 parent e294056 commit 0b22ca3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
8 changes: 8 additions & 0 deletions server/src/main/java/org/elasticsearch/common/joda/Joda.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.io.Writer;
import java.math.BigDecimal;
import java.util.Locale;
import java.util.regex.Pattern;

public class Joda {

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 0b22ca3

Please sign in to comment.