-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Core: Add DateFormatter interface for java time parsing #33467
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 11 commits into
elastic:master
from
spinscale:1809-java-time-fix-epoch-milli-refactoring
Sep 14, 2018
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
34aca34
Core: Add DateFormatter interface for java time parsing
spinscale 1208deb
add javadoc
spinscale cb327eb
Merge branch 'master' into 1809-java-time-fix-epoch-milli-refactoring
spinscale 64de3c1
Merge branch 'master' into 1809-java-time-fix-epoch-milli-refactoring
spinscale 2eba303
Merge branch 'master' into 1809-java-time-fix-epoch-milli-refactoring
spinscale cf1613b
reduce visibilities
spinscale 0950be9
Merge branch 'master' into 1809-java-time-fix-epoch-milli-refactoring
spinscale b05fdce
incorporate review comments
spinscale 754c884
fix broken refactoring
spinscale bfa704e
Merge branch 'master' into 1809-java-time-fix-epoch-milli-refactoring
spinscale f778ae3
Merge branch 'master' into 1809-java-time-fix-epoch-milli-refactoring
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
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
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
133 changes: 133 additions & 0 deletions
133
server/src/main/java/org/elasticsearch/common/time/DateFormatter.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,133 @@ | ||
/* | ||
* 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.time.ZoneId; | ||
import java.time.format.DateTimeParseException; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.time.temporal.TemporalField; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public interface DateFormatter { | ||
|
||
/** | ||
* Try to parse input to a java time TemporalAccessor | ||
* @param input An arbitrary string resembling the string representation of a date or time | ||
* @throws DateTimeParseException If parsing fails, this exception will be thrown. | ||
* Note that it can contained suppressed exceptions when several formatters failed parse this value | ||
* @return The java time object containing the parsed input | ||
*/ | ||
TemporalAccessor parse(String input); | ||
|
||
/** | ||
* Create a copy of this formatter that is configured to parse dates in the specified time zone | ||
* | ||
* @param zoneId The time zone to act on | ||
* @return A copy of the date formatter this has been called on | ||
*/ | ||
DateFormatter withZone(ZoneId zoneId); | ||
|
||
/** | ||
* Print the supplied java time accessor in a string based representation according to this formatter | ||
* | ||
* @param accessor The temporal accessor used to format | ||
* @return The string result for the formatting | ||
*/ | ||
String print(TemporalAccessor accessor); | ||
|
||
/** | ||
* A name based format for this formatter. Can be one of the registered formatters like <code>epoch_millis</code> or | ||
* a configured format like <code>HH:mm:ss</code> | ||
* | ||
* @return The name of this formatter | ||
*/ | ||
String format(); | ||
spinscale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Configure a formatter using default fields for a TemporalAccessor that should be used in case | ||
* the supplied date is not having all of those fields | ||
* | ||
* @param fields A <code>Map<TemporalField, Long></code> of fields to be used as fallbacks | ||
* @return A new date formatter instance, that will use those fields during parsing | ||
*/ | ||
DateFormatter parseDefaulting(Map<TemporalField, Long> fields); | ||
|
||
/** | ||
* Merge several date formatters into a single one. Useful if you need to have several formatters with | ||
* different formats act as one, for example when you specify a | ||
* format like <code>date_hour||epoch_millis</code> | ||
* | ||
* @param formatters The list of date formatters to be merged together | ||
* @return The new date formtter containing the specified date formatters | ||
*/ | ||
static DateFormatter merge(DateFormatter ... formatters) { | ||
return new MergedDateFormatter(formatters); | ||
} | ||
|
||
class MergedDateFormatter implements DateFormatter { | ||
|
||
private final String format; | ||
private final DateFormatter[] formatters; | ||
|
||
MergedDateFormatter(DateFormatter ... formatters) { | ||
this.formatters = formatters; | ||
this.format = Arrays.stream(formatters).map(DateFormatter::format).collect(Collectors.joining("||")); | ||
} | ||
|
||
@Override | ||
public TemporalAccessor parse(String input) { | ||
DateTimeParseException failure = null; | ||
for (DateFormatter formatter : formatters) { | ||
try { | ||
return formatter.parse(input); | ||
} catch (DateTimeParseException e) { | ||
if (failure == null) { | ||
failure = e; | ||
} else { | ||
failure.addSuppressed(e); | ||
} | ||
} | ||
} | ||
throw failure; | ||
} | ||
|
||
@Override | ||
public DateFormatter withZone(ZoneId zoneId) { | ||
return new MergedDateFormatter(Arrays.stream(formatters).map(f -> f.withZone(zoneId)).toArray(DateFormatter[]::new)); | ||
} | ||
|
||
@Override | ||
public String print(TemporalAccessor accessor) { | ||
return formatters[0].print(accessor); | ||
} | ||
|
||
@Override | ||
public String format() { | ||
return format; | ||
} | ||
|
||
@Override | ||
public DateFormatter parseDefaulting(Map<TemporalField, Long> fields) { | ||
return new MergedDateFormatter(Arrays.stream(formatters).map(f -> f.parseDefaulting(fields)).toArray(DateFormatter[]::new)); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.