Core: Don't rely on java time for epoch seconds formatting (#34086)

In order to be compatible with joda time, this adds an epoch seconds
formatter, that is able to parse floating point values.

However joda time discards the floating point values, but still parses
the data, where as this one is able to parse the whole value including
milliseconds.
This commit is contained in:
Alexander Reelsen 2018-09-28 10:53:33 +02:00 committed by GitHub
parent 506c1c2d47
commit bc7d69f74a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 126 additions and 13 deletions

View File

@ -890,17 +890,6 @@ public class DateFormatters {
private static final DateFormatter YEAR = new JavaDateFormatter("year", private static final DateFormatter YEAR = new JavaDateFormatter("year",
new DateTimeFormatterBuilder().appendValue(ChronoField.YEAR).toFormatter(Locale.ROOT)); new DateTimeFormatterBuilder().appendValue(ChronoField.YEAR).toFormatter(Locale.ROOT));
/*
* Returns a formatter for parsing the seconds since the epoch
*/
private static final DateFormatter EPOCH_SECOND = new JavaDateFormatter("epoch_second",
new DateTimeFormatterBuilder().appendValue(ChronoField.INSTANT_SECONDS).toFormatter(Locale.ROOT));
/*
* Parses the milliseconds since/before the epoch
*/
private static final DateFormatter EPOCH_MILLIS = EpochMillisDateFormatter.INSTANCE;
/* /*
* Returns a formatter that combines a full date and two digit hour of * Returns a formatter that combines a full date and two digit hour of
* day. (yyyy-MM-dd'T'HH) * day. (yyyy-MM-dd'T'HH)
@ -1375,9 +1364,9 @@ public class DateFormatters {
} else if ("yearMonthDay".equals(input) || "year_month_day".equals(input)) { } else if ("yearMonthDay".equals(input) || "year_month_day".equals(input)) {
return YEAR_MONTH_DAY; return YEAR_MONTH_DAY;
} else if ("epoch_second".equals(input)) { } else if ("epoch_second".equals(input)) {
return EPOCH_SECOND; return EpochSecondsDateFormatter.INSTANCE;
} else if ("epoch_millis".equals(input)) { } else if ("epoch_millis".equals(input)) {
return EPOCH_MILLIS; return EpochMillisDateFormatter.INSTANCE;
// strict date formats here, must be at least 4 digits for year and two for months and two for day // strict date formats here, must be at least 4 digits for year and two for months and two for day
} else if ("strictBasicWeekDate".equals(input) || "strict_basic_week_date".equals(input)) { } else if ("strictBasicWeekDate".equals(input) || "strict_basic_week_date".equals(input)) {
return STRICT_BASIC_WEEK_DATE; return STRICT_BASIC_WEEK_DATE;

View File

@ -0,0 +1,85 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.common.time;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;
import java.util.Map;
import java.util.regex.Pattern;
public class EpochSecondsDateFormatter implements DateFormatter {
public static DateFormatter INSTANCE = new EpochSecondsDateFormatter();
private static final Pattern SPLIT_BY_DOT_PATTERN = Pattern.compile("\\.");
private EpochSecondsDateFormatter() {}
@Override
public TemporalAccessor parse(String input) {
try {
if (input.contains(".")) {
String[] inputs = SPLIT_BY_DOT_PATTERN.split(input, 2);
Long seconds = Long.valueOf(inputs[0]);
if (inputs[1].length() == 0) {
// this is BWC compatible to joda time, nothing after the dot is allowed
return Instant.ofEpochSecond(seconds, 0).atZone(ZoneOffset.UTC);
}
if (inputs[1].length() > 9) {
throw new DateTimeParseException("too much granularity after dot [" + input + "]", input, 0);
}
Long nanos = new BigDecimal(inputs[1]).movePointRight(9 - inputs[1].length()).longValueExact();
return Instant.ofEpochSecond(seconds, nanos).atZone(ZoneOffset.UTC);
} else {
return Instant.ofEpochSecond(Long.valueOf(input)).atZone(ZoneOffset.UTC);
}
} catch (NumberFormatException e) {
throw new DateTimeParseException("invalid number [" + input + "]", input, 0, e);
}
}
@Override
public DateFormatter withZone(ZoneId zoneId) {
return this;
}
@Override
public String format(TemporalAccessor accessor) {
Instant instant = Instant.from(accessor);
if (instant.getNano() != 0) {
return String.valueOf(instant.getEpochSecond()) + "." + String.valueOf(instant.getNano()).replaceAll("0*$", "");
}
return String.valueOf(instant.getEpochSecond());
}
@Override
public String pattern() {
return "epoch_seconds";
}
@Override
public DateFormatter parseDefaulting(Map<TemporalField, Long> fields) {
return this;
}
}

View File

@ -71,6 +71,8 @@ public class JavaJodaTimeDuellingTests extends ESTestCase {
public void testDuellingFormatsValidParsing() { public void testDuellingFormatsValidParsing() {
assertSameDate("1522332219", "epoch_second"); assertSameDate("1522332219", "epoch_second");
assertSameDate("1522332219.", "epoch_second");
assertSameDate("1522332219.0", "epoch_second");
assertSameDate("0", "epoch_second"); assertSameDate("0", "epoch_second");
assertSameDate("1", "epoch_second"); assertSameDate("1", "epoch_second");
assertSameDate("-1", "epoch_second"); assertSameDate("-1", "epoch_second");

View File

@ -21,6 +21,7 @@ package org.elasticsearch.common.time;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import java.time.Instant;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
@ -56,6 +57,42 @@ public class DateFormattersTests extends ESTestCase {
assertSameFormat(formatter, 1); assertSameFormat(formatter, 1);
} }
// this is not in the duelling tests, because the epoch second parser in joda time drops the milliseconds after the comma
// but is able to parse the rest
// as this feature is supported it also makes sense to make it exact
public void testEpochSecondParser() {
DateFormatter formatter = DateFormatters.forPattern("epoch_second");
assertThat(Instant.from(formatter.parse("1234.567")).toEpochMilli(), is(1234567L));
assertThat(Instant.from(formatter.parse("1234.")).getNano(), is(0));
assertThat(Instant.from(formatter.parse("1234.")).getEpochSecond(), is(1234L));
assertThat(Instant.from(formatter.parse("1234.1")).getNano(), is(100_000_000));
assertThat(Instant.from(formatter.parse("1234.12")).getNano(), is(120_000_000));
assertThat(Instant.from(formatter.parse("1234.123")).getNano(), is(123_000_000));
assertThat(Instant.from(formatter.parse("1234.1234")).getNano(), is(123_400_000));
assertThat(Instant.from(formatter.parse("1234.12345")).getNano(), is(123_450_000));
assertThat(Instant.from(formatter.parse("1234.123456")).getNano(), is(123_456_000));
assertThat(Instant.from(formatter.parse("1234.1234567")).getNano(), is(123_456_700));
assertThat(Instant.from(formatter.parse("1234.12345678")).getNano(), is(123_456_780));
assertThat(Instant.from(formatter.parse("1234.123456789")).getNano(), is(123_456_789));
DateTimeParseException e = expectThrows(DateTimeParseException.class, () -> formatter.parse("1234.1234567890"));
assertThat(e.getMessage(), is("too much granularity after dot [1234.1234567890]"));
e = expectThrows(DateTimeParseException.class, () -> formatter.parse("1234.123456789013221"));
assertThat(e.getMessage(), is("too much granularity after dot [1234.123456789013221]"));
e = expectThrows(DateTimeParseException.class, () -> formatter.parse("abc"));
assertThat(e.getMessage(), is("invalid number [abc]"));
e = expectThrows(DateTimeParseException.class, () -> formatter.parse("1234.abc"));
assertThat(e.getMessage(), is("invalid number [1234.abc]"));
// different zone, should still yield the same output, as epoch is time zone independent
ZoneId zoneId = randomZone();
DateFormatter zonedFormatter = formatter.withZone(zoneId);
assertThatSameDateTime(formatter, zonedFormatter, randomLongBetween(-100_000_000, 100_000_000));
assertSameFormat(formatter, randomLongBetween(-100_000_000, 100_000_000));
assertThat(formatter.format(Instant.ofEpochSecond(1234, 567_000_000)), is("1234.567"));
}
public void testEpochMilliParsersWithDifferentFormatters() { public void testEpochMilliParsersWithDifferentFormatters() {
DateFormatter formatter = DateFormatters.forPattern("strict_date_optional_time||epoch_millis"); DateFormatter formatter = DateFormatters.forPattern("strict_date_optional_time||epoch_millis");
TemporalAccessor accessor = formatter.parse("123"); TemporalAccessor accessor = formatter.parse("123");