Skip to content
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

Add method that parses dates without throwing exceptions #83801

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

public interface DateFormatter {

Expand All @@ -30,6 +31,14 @@ public interface DateFormatter {
*/
TemporalAccessor parse(String input);

/**
* Try to parse input to a java time TemporalAccessor
* @param input An arbitrary string resembling the string representation of a date or time
* @return Tuple containing a boolean value indicating parsing success or failure and a java time object containing
* the parsed input in the case of successful parsing
*/
Optional<TemporalAccessor> parseWithoutException(String input);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think we should add another parse method. If this is more performant, then let's change the implementation of parse.


/**
* Parse the given input into millis-since-epoch.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -176,6 +177,19 @@ public TemporalAccessor parse(String input) {
}
}

@Override
public Optional<TemporalAccessor> parseWithoutException(String input) {
for (DateTimeFormatter formatter : parsers) {
ParsePosition pos = new ParsePosition(0);
Object object = formatter.toFormat().parseObject(input, pos);
if (parsingSucceeded(object, input, pos)) {
return Optional.of((TemporalAccessor) object);
}
}

return Optional.empty();
}

/**
* Attempt parsing the input without throwing exception. If multiple parsers are provided,
* it will continue iterating if the previous parser failed. The pattern must fully match, meaning whole input was used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,19 @@
public class DateFormattersTests extends ESTestCase {

private void assertParseException(String input, String format) {
assertParseException(input, format, true);
}

private void assertParseException(String input, String format, boolean testParseWithoutException) {

DateFormatter javaTimeFormatter = DateFormatter.forPattern(format);
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> javaTimeFormatter.parse(input));
assertThat(e.getMessage(), containsString(input));
assertThat(e.getMessage(), containsString(format));
if (testParseWithoutException) {
var result = javaTimeFormatter.parseWithoutException(input);
assertTrue(result.isEmpty());
}
}

private void assertParses(String input, String format) {
Expand All @@ -53,6 +62,11 @@ private void assertParses(String input, DateFormatter formatter) {
ZonedDateTime zonedDateTime = DateFormatters.from(javaTimeAccessor);

assertThat(zonedDateTime, notNullValue());

var result = formatter.parseWithoutException(input);
assertFalse(result.isEmpty());
ZonedDateTime zdt2 = DateFormatters.from(result.get());
assertThat(zonedDateTime, equalTo(zdt2));
}

private void assertDateMathEquals(String text, String pattern) {
Expand Down Expand Up @@ -426,8 +440,8 @@ public void testFractionalSeconds() {
}

public void testIncorrectFormat() {
assertParseException("2021-01-01T23-35-00Z", "strict_date_optional_time||epoch_millis");
assertParseException("2021-01-01T23-35-00Z", "strict_date_optional_time");
assertParseException("2021-01-01T23-35-00Z", "strict_date_optional_time||epoch_millis", false);
assertParseException("2021-01-01T23-35-00Z", "strict_date_optional_time", false);
}

public void testMinMillis() {
Expand Down