-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Core: Don't rely on java time for epoch seconds formatting #34086
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
Merged
spinscale
merged 7 commits into
elastic:master
from
spinscale:1809-fix-epoch-seconds-formatter
Sep 28, 2018
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5d2622a
Core: Don't rely on java time for epoch seconds formatting
spinscale f1b2ac9
Merge branch 'master' into 1809-fix-epoch-seconds-formatter
spinscale 3240055
incorporate review comments
spinscale 54cd692
incorporate review comments
spinscale f0388a3
remove epoch_seconds formatter and depreciation warning within this P…
spinscale 8f34df6
remote leftover debugging logger statements
spinscale 29bba14
remove unused deprecation formatter
spinscale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This seems trappy...we should at least have a comment why this is ok, but it seems like we could drop timezones?
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.
the java-time branch will create new objects with a timezone for all the date formatters (same needs to be done for the locale). I will create a separate PR for that after this one.