Skip to content

Commit

Permalink
[CALCITE-6215] Support century format datetime/timestamp in pg
Browse files Browse the repository at this point in the history
  • Loading branch information
caicancai authored and mihaibudiu committed Jan 25, 2024
1 parent 26b5e9b commit 21d263b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions babel/src/test/resources/sql/postgresql.iq
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ EXPR$0
2022-06-03 12:15:48.678
!ok

select to_char(timestamp '2022-06-03 12:15:48.678', 'CC');
EXPR$0
21
!ok

# -----------------------------------------------------------------------------
# Posix regex

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
* @see FormatModels#DEFAULT
*/
public enum FormatElementEnum implements FormatElement {
CC("cc", "century (2 digits) (the twenty-first century starts on 2001-01-01)") {
@Override public void format(StringBuilder sb, Date date) {
final Calendar calendar = Work.get().calendar;
calendar.setTime(date);
sb.append(String.format(Locale.ROOT, "%2d", calendar.get(Calendar.YEAR) / 100 + 1));
}
},
D("F", "The weekday (Monday as the first day of the week) as a decimal number (1-7)") {
@Override public void format(StringBuilder sb, Date date) {
final Calendar calendar = Work.get().calendar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.apache.calcite.util.format.FormatElementEnum.CC;
import static org.apache.calcite.util.format.FormatElementEnum.D;
import static org.apache.calcite.util.format.FormatElementEnum.DAY;
import static org.apache.calcite.util.format.FormatElementEnum.DD;
Expand Down Expand Up @@ -161,6 +162,7 @@ MI, literalElement(":"), SS, literalElement(" "),
map.put("Month", MONTH);
map.put("Mon", MON);
map.put("MM", MM);
map.put("CC", CC);
map.put("DDD", DDD);
map.put("DD", DD);
map.put("D", D);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
*/
class FormatElementEnumTest {

@Test void testCC() {
assertFormatElement(FormatElementEnum.CC, "2014-09-30T10:00:00Z", "21");
}

@Test void testDay() {
assertFormatElement(FormatElementEnum.DAY, "2014-09-30T10:00:00Z", "Tuesday");
}
Expand Down

0 comments on commit 21d263b

Please sign in to comment.