Skip to content

Commit 0b22ca3

Browse files
authored
Core: Deprecate use of scientific notation in epoch time parsing (#36691)
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.
1 parent e294056 commit 0b22ca3

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

server/src/main/java/org/elasticsearch/common/joda/Joda.java

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.io.Writer;
4848
import java.math.BigDecimal;
4949
import java.util.Locale;
50+
import java.util.regex.Pattern;
5051

5152
public class Joda {
5253

@@ -321,6 +322,8 @@ public DateTimeField getField(Chronology chronology) {
321322

322323
public static class EpochTimeParser implements DateTimeParser {
323324

325+
private static final Pattern scientificNotation = Pattern.compile("[Ee]");
326+
324327
private final boolean hasMilliSecondPrecision;
325328

326329
public EpochTimeParser(boolean hasMilliSecondPrecision) {
@@ -348,6 +351,11 @@ public int parseInto(DateTimeParserBucket bucket, String text, int position) {
348351
int factor = hasMilliSecondPrecision ? 1 : 1000;
349352
try {
350353
long millis = new BigDecimal(text).longValue() * factor;
354+
// check for deprecation, but after it has parsed correctly so the "e" isn't from something else
355+
if (scientificNotation.matcher(text).find()) {
356+
deprecationLogger.deprecatedAndMaybeLog("epoch-scientific-notation", "Use of scientific notation" +
357+
"in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch.");
358+
}
351359
DateTime dt = new DateTime(millis, DateTimeZone.UTC);
352360
bucket.saveField(DateTimeFieldType.year(), dt.getYear());
353361
bucket.saveField(DateTimeFieldType.monthOfYear(), dt.getMonthOfYear());

server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java

-2
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,11 @@ public void testDuellingFormatsValidParsing() {
7777
assertSameDate("1", "epoch_second");
7878
assertSameDate("-1", "epoch_second");
7979
assertSameDate("-1522332219", "epoch_second");
80-
assertSameDate("1.0e3", "epoch_second");
8180
assertSameDate("1522332219321", "epoch_millis");
8281
assertSameDate("0", "epoch_millis");
8382
assertSameDate("1", "epoch_millis");
8483
assertSameDate("-1", "epoch_millis");
8584
assertSameDate("-1522332219321", "epoch_millis");
86-
assertSameDate("1.0e3", "epoch_millis");
8785

8886
assertSameDate("20181126", "basic_date");
8987
assertSameDate("20181126T121212.123Z", "basic_date_time");

server/src/test/java/org/elasticsearch/common/joda/SimpleJodaTests.java

+9
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,15 @@ public void testDeprecatedFormatSpecifiers() {
753753
" next major version of Elasticsearch. Prefix your date format with '8' to use the new specifier.");
754754
}
755755

756+
public void testDeprecatedScientificNotation() {
757+
assertValidDateFormatParsing("epoch_second", "1.234e5", "123400");
758+
assertWarnings("Use of scientific notation" +
759+
"in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch.");
760+
assertValidDateFormatParsing("epoch_millis", "1.234e5", "123400");
761+
assertWarnings("Use of scientific notation" +
762+
"in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch.");
763+
}
764+
756765
private void assertValidDateFormatParsing(String pattern, String dateToParse) {
757766
assertValidDateFormatParsing(pattern, dateToParse, dateToParse);
758767
}

0 commit comments

Comments
 (0)