-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Core: Fix epoch millis java time formatter #33302
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,10 +25,12 @@ | |
import java.time.DayOfWeek; | ||
import java.time.Instant; | ||
import java.time.LocalDate; | ||
import java.time.ZoneId; | ||
import java.time.ZoneOffset; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeFormatterBuilder; | ||
import java.time.format.DateTimeParseException; | ||
import java.time.format.ResolverStyle; | ||
import java.time.format.SignStyle; | ||
import java.time.temporal.ChronoField; | ||
|
@@ -879,11 +881,42 @@ public class DateFormatters { | |
|
||
/* | ||
* Returns a formatter for parsing the milliseconds since the epoch | ||
* This one needs a custom implementation, because the standard date formatter can not parse negative values | ||
* or anything +- 999 milliseconds around the eopch | ||
* | ||
* This implementation just resorts to parsing the input directly to an Instant by trying to parse a number. | ||
*/ | ||
private static final CompoundDateTimeFormatter EPOCH_MILLIS = new CompoundDateTimeFormatter(new DateTimeFormatterBuilder() | ||
private static final DateTimeFormatter EPOCH_MILLIS_FORMATTER = new DateTimeFormatterBuilder() | ||
.appendValue(ChronoField.INSTANT_SECONDS, 1, 19, SignStyle.NEVER) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The SignStyle made me curious so I tried outputting a parsed negative instance using this formatter like:
This gives me
So the parsed instance is what I would expect, but the formatted value looks odd. I think this means the format method might be overwritten somehow, or maybe this can be done with the DateTimeFormatterBuilder()? In any case, I think a test that parses negative values and outputs them using this formatter and checks for equality might be useful in any case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call. It seems that the date time formatter cannot deal with negative dates... overwriting the |
||
.appendValue(ChronoField.MILLI_OF_SECOND, 3) | ||
.toFormatter(Locale.ROOT)); | ||
.toFormatter(Locale.ROOT); | ||
|
||
private static final class EpochDateTimeFormatter extends CompoundDateTimeFormatter { | ||
|
||
EpochDateTimeFormatter() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: could even be private I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the class was already private... but fixed it for both ctors two as well |
||
super(EPOCH_MILLIS_FORMATTER); | ||
} | ||
|
||
EpochDateTimeFormatter(ZoneId zoneId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: could even be private I think |
||
super(EPOCH_MILLIS_FORMATTER.withZone(zoneId)); | ||
} | ||
|
||
@Override | ||
public TemporalAccessor parse(String input) { | ||
try { | ||
return Instant.ofEpochMilli(Long.valueOf(input)).atZone(ZoneOffset.UTC); | ||
} catch (NumberFormatException e) { | ||
throw new DateTimeParseException("invalid number", input, 0, e); | ||
} | ||
} | ||
|
||
@Override | ||
public CompoundDateTimeFormatter withZone(ZoneId zoneId) { | ||
return new EpochDateTimeFormatter(zoneId); | ||
} | ||
} | ||
|
||
private static final CompoundDateTimeFormatter EPOCH_MILLIS = new EpochDateTimeFormatter(); | ||
|
||
/* | ||
* Returns a formatter that combines a full date and two digit hour of | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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 org.elasticsearch.test.ESTestCase; | ||
|
||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeParseException; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
public class DateFormattersTests extends ESTestCase { | ||
|
||
// the epoch milli parser is a bit special, as it does not use date formatter, see comments in DateFormatters | ||
public void testEpochMilliParser() { | ||
CompoundDateTimeFormatter formatter = DateFormatters.forPattern("epoch_millis"); | ||
|
||
DateTimeParseException e = expectThrows(DateTimeParseException.class, () -> formatter.parse("invalid")); | ||
assertThat(e.getMessage(), containsString("invalid number")); | ||
|
||
// different zone, should still yield the same output, as epoch is time zoned independent | ||
ZoneId zoneId = randomZone(); | ||
CompoundDateTimeFormatter zonedFormatter = formatter.withZone(zoneId); | ||
assertThat(zonedFormatter.printer.getZone(), is(zoneId)); | ||
|
||
// test with negative and non negative values | ||
assertThatSameDateTime(formatter, zonedFormatter, String.valueOf(randomNonNegativeLong() * -1)); | ||
assertThatSameDateTime(formatter, zonedFormatter, String.valueOf(randomNonNegativeLong())); | ||
assertThatSameDateTime(formatter, zonedFormatter, String.valueOf(0)); | ||
assertThatSameDateTime(formatter, zonedFormatter, String.valueOf(-1)); | ||
assertThatSameDateTime(formatter, zonedFormatter, String.valueOf(1)); | ||
} | ||
|
||
private void assertThatSameDateTime(CompoundDateTimeFormatter formatter, CompoundDateTimeFormatter zonedFormatter, String value) { | ||
ZonedDateTime formatterZonedDateTime = DateFormatters.toZonedDateTime(formatter.parse(value)); | ||
ZonedDateTime zonedFormatterZonedDateTime = DateFormatters.toZonedDateTime(zonedFormatter.parse(value)); | ||
assertThat(formatterZonedDateTime.toInstant().toEpochMilli(), is(zonedFormatterZonedDateTime.toInstant().toEpochMilli())); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/eopch/epoch/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed