-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
4 changed files
with
126 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
server/src/main/java/org/elasticsearch/common/time/EpochSecondsDateFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters