Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5651d8d

Browse files
Georg Traarmergify[bot]
authored andcommitted
Fixed date_format() for day of month specifier
1 parent 6a04475 commit 5651d8d

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

docs/appendices/release-notes/unreleased.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,7 @@ Fixes
143143
- Fixed an issue that caused ``UNION ALL`` statements to succeed or throw
144144
unexpected exceptions when the ``SELECT`` results for ``UNION ALL`` included
145145
object types with identically named but differently typed sub-columns.
146+
147+
- Fixed an issue that caused ``date_format()`` to return wrong values when used
148+
with the ``%D`` specifier (day of month as ordinal number) for 11th, 12th and
149+
13th.

server/src/main/java/io/crate/expression/scalar/TimestampFormatter.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,20 @@ public String format(DateTime timestamp) {
7878
builder.append(n);
7979
if (n >= 11 && n <= 13) {
8080
builder.append("th");
81-
}
82-
switch (n % 10) {
83-
case 1:
84-
builder.append("st");
85-
break;
86-
case 2:
87-
builder.append("nd");
88-
break;
89-
case 3:
90-
builder.append("rd");
91-
break;
92-
default:
93-
builder.append("th");
81+
} else {
82+
switch (n % 10) {
83+
case 1:
84+
builder.append("st");
85+
break;
86+
case 2:
87+
builder.append("nd");
88+
break;
89+
case 3:
90+
builder.append("rd");
91+
break;
92+
default:
93+
builder.append("th");
94+
}
9495
}
9596
return builder.toString();
9697
}

server/src/test/java/io/crate/expression/scalar/DateFormatFunctionTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,11 @@ public void testInvalidFormats() {
227227
Literal.of("%t%%%Z"),
228228
Literal.of(DataTypes.TIMESTAMPZ, DataTypes.TIMESTAMPZ.implicitCast("2000-01-01")));
229229
}
230+
231+
@Test
232+
public void testDayOfMonthWithEnglishSuffix() throws Exception {
233+
assertNormalize("date_format('%D', '2021-10-01')", isLiteral("1st"));
234+
assertNormalize("date_format('%D', '2021-10-12')", isLiteral("12th"));
235+
assertNormalize("date_format('%D', '2021-10-22')", isLiteral("22nd"));
236+
}
230237
}

0 commit comments

Comments
 (0)