Skip to content
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 @@ -295,29 +295,29 @@ private static LocalDateTime firstDayOfWeek(LocalDateTime dateTime) {
@ExecFunction(name = "date_format")
public static Expression dateFormat(DateLiteral date, StringLikeLiteral format) {
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter().format(
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter(Locale.US).format(
java.time.LocalDate.of(((int) date.getYear()), ((int) date.getMonth()), ((int) date.getDay()))));
}

@ExecFunction(name = "date_format")
public static Expression dateFormat(DateTimeLiteral date, StringLikeLiteral format) {
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter().format(
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter(Locale.US).format(
java.time.LocalDateTime.of(((int) date.getYear()), ((int) date.getMonth()), ((int) date.getDay()),
((int) date.getHour()), ((int) date.getMinute()), ((int) date.getSecond()))));
}

@ExecFunction(name = "date_format")
public static Expression dateFormat(DateV2Literal date, StringLikeLiteral format) {
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter().format(
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter(Locale.US).format(
java.time.LocalDate.of(((int) date.getYear()), ((int) date.getMonth()), ((int) date.getDay()))));
}

@ExecFunction(name = "date_format")
public static Expression dateFormat(DateTimeV2Literal date, StringLikeLiteral format) {
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter().format(
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter(Locale.US).format(
java.time.LocalDateTime.of(((int) date.getYear()), ((int) date.getMonth()), ((int) date.getDay()),
((int) date.getHour()), ((int) date.getMinute()), ((int) date.getSecond()))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static DateTimeFormatterBuilder formatBuilder(String pattern) throws Anal
break;
case 'h': // %h Hour (01..12)
case 'I': // %I Hour (01..12)
builder.appendValue(ChronoField.HOUR_OF_AMPM, 2);
builder.appendValue(ChronoField.CLOCK_HOUR_OF_AMPM, 2);
break;
case 'i': // %i Minutes, numeric (00..59)
builder.appendValue(ChronoField.MINUTE_OF_HOUR, 2);
Expand All @@ -80,7 +80,7 @@ public static DateTimeFormatterBuilder formatBuilder(String pattern) throws Anal
builder.appendValue(ChronoField.HOUR_OF_DAY);
break;
case 'l': // %l Hour (1..12)
builder.appendValue(ChronoField.HOUR_OF_AMPM);
builder.appendValue(ChronoField.CLOCK_HOUR_OF_AMPM);
break;
case 'M': // %M Month name (January..December)
builder.appendText(ChronoField.MONTH_OF_YEAR, TextStyle.FULL);
Expand All @@ -92,8 +92,10 @@ public static DateTimeFormatterBuilder formatBuilder(String pattern) throws Anal
builder.appendText(ChronoField.AMPM_OF_DAY);
break;
case 'r': // %r Time, 12-hour (hh:mm:ss followed by AM or PM)
builder.appendValue(ChronoField.HOUR_OF_AMPM, 2)
builder.appendValue(ChronoField.CLOCK_HOUR_OF_AMPM, 2)
.appendLiteral(':')
.appendValue(ChronoField.MINUTE_OF_HOUR, 2)
.appendLiteral(':')
.appendValue(ChronoField.SECOND_OF_MINUTE, 2)
.appendLiteral(' ')
.appendText(ChronoField.AMPM_OF_DAY, TextStyle.FULL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@ suite("fold_constant_date_arithmatic") {
testFoldConst("select substr(current_date(), 1, 10);")
testFoldConst("select substr(current_timestamp(), 1, 10);")
testFoldConst("select substr(current_timestamp(3), 1, 10);")

testFoldConst("SELECT date_format('2020-12-01 00:00:30.01', '%h');")
testFoldConst("SELECT date_format('2020-12-01 00:00:30.01', '%I');")
testFoldConst("SELECT date_format('2020-12-01 00:00:30.01', '%l');")
testFoldConst("SELECT date_format('2020-12-01 00:00:30.01', '%r');")
testFoldConst("SELECT date_format('2020-12-01 12:00:30.01', '%h');")
testFoldConst("SELECT date_format('2020-12-01 12:00:30.01', '%I');")
testFoldConst("SELECT date_format('2020-12-01 12:00:30.01', '%l');")
testFoldConst("SELECT date_format('2020-12-01 12:00:30.01', '%r');")
}
Loading