Skip to content

[7.x] Allow partial parsing dates (#46814) #47872

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
merged 2 commits into from
Oct 11, 2019
Merged
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 @@ -59,10 +59,14 @@ public class DateFormatters {

private static final DateTimeFormatter STRICT_YEAR_MONTH_DAY_FORMATTER = new DateTimeFormatterBuilder()
.appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
.optionalStart()
.appendLiteral("-")
.appendValue(MONTH_OF_YEAR, 2, 2, SignStyle.NOT_NEGATIVE)
.optionalStart()
.appendLiteral('-')
.appendValue(DAY_OF_MONTH, 2, 2, SignStyle.NOT_NEGATIVE)
.optionalEnd()
.optionalEnd()
.toFormatter(Locale.ROOT);

private static final DateTimeFormatter STRICT_HOUR_MINUTE_SECOND_FORMATTER = new DateTimeFormatterBuilder()
Expand Down Expand Up @@ -910,12 +914,14 @@ public class DateFormatters {

private static final DateTimeFormatter DATE_FORMATTER = new DateTimeFormatterBuilder()
.appendValue(ChronoField.YEAR, 1, 5, SignStyle.NORMAL)
.optionalStart()
.appendLiteral('-')
.appendValue(MONTH_OF_YEAR, 1, 2, SignStyle.NOT_NEGATIVE)
.optionalStart()
.appendLiteral('-')
.appendValue(DAY_OF_MONTH, 1, 2, SignStyle.NOT_NEGATIVE)
.optionalEnd()
.optionalEnd()
.toFormatter(Locale.ROOT);

private static final DateTimeFormatter HOUR_MINUTE_FORMATTER = new DateTimeFormatterBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,37 @@
import java.util.Locale;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsEqual.equalTo;

public class JavaJodaTimeDuellingTests extends ESTestCase {
@Override
protected boolean enableWarningsCheck() {
return false;
}
// date_optional part of a parser names "strict_date_optional_time" or "date_optional"time
// means that date part can be partially parsed.
public void testPartialParsing() {
assertSameDateAs("2001", "strict_date_optional_time_nanos", "strict_date_optional_time");
assertSameDateAs("2001-01", "strict_date_optional_time_nanos", "strict_date_optional_time");
assertSameDateAs("2001-01-01", "strict_date_optional_time_nanos", "strict_date_optional_time");

assertSameDate("2001", "strict_date_optional_time");
assertSameDate("2001-01", "strict_date_optional_time");
assertSameDate("2001-01-01", "strict_date_optional_time");

assertSameDate("2001", "date_optional_time");
assertSameDate("2001-01", "date_optional_time");
assertSameDate("2001-01-01", "date_optional_time");


assertSameDateAs("2001", "iso8601", "strict_date_optional_time");
assertSameDateAs("2001-01", "iso8601", "strict_date_optional_time");
assertSameDateAs("2001-01-01", "iso8601", "strict_date_optional_time");

assertSameDate("9999","date_optional_time||epoch_second");
}

public void testCompositeDateMathParsing(){
//in all these examples the second pattern will be used
assertDateMathEquals("2014-06-06T12:01:02.123", "yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSS");
Expand Down Expand Up @@ -909,4 +936,10 @@ private void assertDateMathEquals(String text, String pattern) {

assertEquals(gotMillisJoda, gotMillisJava);
}

private void assertSameDateAs(String input, String javaPattern, String jodaPattern) {
DateFormatter javaFormatter = DateFormatter.forPattern(javaPattern);
DateFormatter jodaFormatter = Joda.forPattern(jodaPattern);
assertSameDate(input, javaPattern, jodaFormatter, javaFormatter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,11 @@ public void testTimestamps() {
long datetime = parser.parse("1418248078", () -> 0).toEpochMilli();
assertDateEquals(datetime, "1418248078", "2014-12-10T21:47:58.000");

// a timestamp before 10000 is a year
assertDateMathEquals("9999", "1970-01-01T00:00:09.999Z");
// 10000 is also a year, breaking bwc, used to be a timestamp
assertDateMathEquals("10000", "1970-01-01T00:00:10.000Z");
// a timestamp before 100000 is a year
assertDateMathEquals("9999", "9999-01-01T00:00:00.000");
assertDateMathEquals("10000", "10000-01-01T00:00:00.000");
assertDateMathEquals("100000", "1970-01-01T00:01:40.000");

// but 10000 with T is still a date format
assertDateMathEquals("10000-01-01T", "10000-01-01T00:00:00.000");
}
Expand Down