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

Core: Revert back to joda's multi date formatters #36814

Merged
merged 5 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions server/src/main/java/org/elasticsearch/common/joda/Joda.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,27 @@ public static JodaDateFormatter forPattern(String input) {
formatter = StrictISODateTimeFormat.yearMonth();
} else if ("strictYearMonthDay".equals(input) || "strict_year_month_day".equals(input)) {
formatter = StrictISODateTimeFormat.yearMonthDay();
} else if (Strings.hasLength(input) && input.contains("||")) {
String[] formats = Strings.delimitedListToStringArray(input, "||");
DateTimeParser[] parsers = new DateTimeParser[formats.length];

if (formats.length == 1) {
formatter = forPattern(input).parser;
} else {
DateTimeFormatter dateTimeFormatter = null;
for (int i = 0; i < formats.length; i++) {
JodaDateFormatter currentFormatter = forPattern(formats[i]);
DateTimeFormatter currentParser = currentFormatter.parser;
if (dateTimeFormatter == null) {
dateTimeFormatter = currentFormatter.printer;
}
parsers[i] = currentParser.getParser();
}

DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder()
.append(dateTimeFormatter.withZone(DateTimeZone.UTC).getPrinter(), parsers);
formatter = builder.toFormatter();
}
} else {
try {
maybeLogJodaDeprecation(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,17 @@ static DateFormatter forPattern(String input) {
if (Strings.hasLength(input) == false) {
throw new IllegalArgumentException("No date pattern provided");
}
if (input.startsWith("8") == false) {
return Joda.forPattern(input);
}

// force java 8 date format
List<DateFormatter> formatters = new ArrayList<>();
for (String pattern : Strings.delimitedListToStringArray(input, "||")) {
if (Strings.hasLength(input) == false) {
throw new IllegalArgumentException("Cannot have empty element in multi date format pattern: " + input);
}
final DateFormatter formatter;
if (pattern.startsWith("8")) {
// force java 8 date format
formatter = DateFormatters.forPattern(pattern.substring(1));
} else {
formatter = Joda.forPattern(pattern);
}
formatters.add(formatter);
formatters.add(DateFormatters.forPattern(pattern.substring(1)));
Copy link
Contributor

Choose a reason for hiding this comment

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

this needs a check for each of the pattern if it starts with 8 in order to return the proper joda time pattern for sth like 8date_optional_time||date_optional_time

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, this is leftover. We won't be able to have mixed joda/java formats, so I think only having 8 as the very first character is sufficient (otherwise it would be redundant). I'll move the substring to outside the loop.

}

if (formatters.size() == 1) {
Expand Down