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

Support ISO 8601 Format in Date Format. #460

Merged
merged 8 commits into from
Mar 7, 2022
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 @@ -79,6 +79,8 @@ interface DateTimeFormatHandler {
.build();

private static final Pattern pattern = Pattern.compile("%.");
private static final Pattern CHARACTERS_WITH_NO_MOD_LITERAL_BEHIND_PATTERN
= Pattern.compile("(?<!%)[a-zA-Z&&[^aydmshiHIMYDSEL]]+");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

private static final String MOD_LITERAL = "%";

private DateTimeFormatterUtil() {
Expand All @@ -92,7 +94,15 @@ private DateTimeFormatterUtil() {
*/
static ExprValue getFormattedDate(ExprValue dateExpr, ExprValue formatExpr) {
final LocalDateTime date = dateExpr.datetimeValue();
final Matcher matcher = pattern.matcher(formatExpr.stringValue());
final StringBuffer cleanFormat = new StringBuffer();
final Matcher m = CHARACTERS_WITH_NO_MOD_LITERAL_BEHIND_PATTERN
.matcher(formatExpr.stringValue());
while (m.find()) {
m.appendReplacement(cleanFormat,String.format("'%s'", m.group()));
}
m.appendTail(cleanFormat);

final Matcher matcher = pattern.matcher(cleanFormat.toString());
final StringBuffer format = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(format,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,18 @@ public void setup() {
new DateFormatTester("2008-12-31",
ImmutableList.of("%v","%V","%u","%U"),
ImmutableList.of("53","52","53","52")
)
),
new DateFormatTester("1998-01-31 13:14:15.012345",
Copy link
Collaborator

Choose a reason for hiding this comment

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

add not-match cases? e.g. "%Y-%m-%da%Ta"

Copy link
Member Author

Choose a reason for hiding this comment

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

Added few more testcases.

ImmutableList.of("%Y-%m-%dT%TZ"),
ImmutableList.of("1998-01-31T13:14:15Z")
),
new DateFormatTester("1998-01-31 13:14:15.012345",
ImmutableList.of("%Y-%m-%da %T a"),
ImmutableList.of("1998-01-31PM 13:14:15 PM")
),
new DateFormatTester("1998-01-31 13:14:15.012345",
ImmutableList.of("%Y-%m-%db %T b"),
ImmutableList.of("1998-01-31b 13:14:15 b"))
);

@AllArgsConstructor
Expand Down
2 changes: 2 additions & 0 deletions docs/user/dql/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,8 @@ Usage: date_format(date, format) formats the date argument using the specifiers
- A literal % character
* - %x
- x, for any “x” not listed above
* - x
- x, for any smallcase/uppercase alphabet except [aydmshiHIMYDSEL]

Argument type: STRING/DATE/DATETIME/TIMESTAMP, STRING

Expand Down
2 changes: 2 additions & 0 deletions docs/user/ppl/functions/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ Usage: date_format(date, format) formats the date argument using the specifiers
- A literal % character
* - %x
- x, for any “x” not listed above
* - x
- x, for any smallcase/uppercase alphabet except [aydmshiHIMYDSEL]

Argument type: STRING/DATE/DATETIME/TIMESTAMP, STRING

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,4 +448,19 @@ public void testDateFormat() throws IOException {
String dateFormatted = "4 4 4 4 Saturday 6 1998 1998 1998 98";
verifyDateFormat(date, "date", dateFormat, dateFormatted);
}


@Test
public void testDateFormatISO8601() throws IOException {
String timestamp = "1998-01-31 13:14:15.012345";
String timestampFormat = "%Y-%m-%dT%TZ";
String timestampFormatted = "1998-01-31T13:14:15Z";
verifyDateFormat(timestamp, "timestamp", timestampFormat, timestampFormatted);

String date = "1998-01-31";
String dateFormat = "%Y-%m-%dT%TZ";
String dateFormatted = "1998-01-31T00:00:00Z";
verifyDateFormat(date, "date", dateFormat, dateFormatted);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ void should_build_bucket_with_parse_expression() {
+ " \"lang\" : \"opensearch_query_expression\"\n"
+ " },\n"
+ " \"missing_bucket\" : true,\n"
+ " \"missing_order\" : \"first\",\n"
+ " \"order\" : \"asc\"\n"
+ " }\n"
+ "}",
Expand Down