Skip to content

Commit 6780d72

Browse files
authored
Implement actual locale-aware datetime formatting (#932)
1 parent accafe5 commit 6780d72

3 files changed

Lines changed: 14 additions & 9 deletions

File tree

src/main/java/com/hubspot/jinjava/objects/date/StrftimeFormatter.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.hubspot.jinjava.objects.date;
22

3+
import static com.hubspot.jinjava.objects.date.StrftimeFormatter.ConversionComponent.localized;
34
import static com.hubspot.jinjava.objects.date.StrftimeFormatter.ConversionComponent.pattern;
45

56
import com.google.common.collect.ImmutableMap;
@@ -33,7 +34,7 @@ public class StrftimeFormatter {
3334
.put('A', pattern("EEEE"))
3435
.put('b', pattern("MMM"))
3536
.put('B', pattern("MMMM"))
36-
.put('c', pattern("EEE MMM dd HH:mm:ss yyyy"))
37+
.put('c', localized(FormatStyle.MEDIUM, FormatStyle.MEDIUM))
3738
.put('d', pattern("dd"))
3839
.put('e', pattern("d")) // The day of the month like with %d, but padded with blank (range 1 through 31).
3940
.put('f', pattern("SSSSSS"))
@@ -50,8 +51,8 @@ public class StrftimeFormatter {
5051
.put('U', pattern("ww"))
5152
.put('w', pattern("e"))
5253
.put('W', pattern("ww"))
53-
.put('x', pattern("MM/dd/yy"))
54-
.put('X', pattern("HH:mm:ss"))
54+
.put('x', localized(FormatStyle.SHORT, null))
55+
.put('X', localized(null, FormatStyle.MEDIUM))
5556
.put('y', pattern("yy"))
5657
.put('Y', pattern("yyyy"))
5758
.put('z', pattern("Z"))
@@ -176,5 +177,9 @@ static ConversionComponent pattern(String targetPattern) {
176177
stripLeadingZero ? targetPattern.substring(1) : targetPattern
177178
);
178179
}
180+
181+
static ConversionComponent localized(FormatStyle dateStyle, FormatStyle timeStyle) {
182+
return (builder, stripLeadingZero) -> builder.appendLocalized(dateStyle, timeStyle);
183+
}
179184
}
180185
}

src/test/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void itConvertsToLocaleSpecificDateTimeFormat() {
116116
"en-US"
117117
)
118118
)
119-
.isEqualTo("10/11/18 13:09:45 - Thu Oct 11 13:09:45 2018");
119+
.isEqualTo("10/11/18 1:09:45 PM - Oct 11, 2018, 1:09:45 PM");
120120
assertThat(
121121
filter.filter(
122122
1539277785000L,
@@ -126,7 +126,7 @@ public void itConvertsToLocaleSpecificDateTimeFormat() {
126126
"de-DE"
127127
)
128128
)
129-
.isEqualTo("10/11/18 13:09:45 - Do. Okt. 11 13:09:45 2018");
129+
.isEqualTo("11.10.18 13:09:45 - 11.10.2018, 13:09:45");
130130
}
131131

132132
@Test

src/test/java/com/hubspot/jinjava/objects/date/StrftimeFormatterTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ public void testWithNoPcts() {
5454

5555
@Test
5656
public void testDateTime() {
57-
assertThat(StrftimeFormatter.format(d, "%c")).isEqualTo("Wed Nov 06 14:22:00 2013");
57+
assertThat(StrftimeFormatter.format(d, "%c")).isEqualTo("Nov 6, 2013, 2:22:00 PM");
5858
}
5959

6060
@Test
6161
public void testDate() {
62-
assertThat(StrftimeFormatter.format(d, "%x")).isEqualTo("11/06/13");
62+
assertThat(StrftimeFormatter.format(d, "%x")).isEqualTo("11/6/13");
6363
}
6464

6565
@Test
@@ -69,12 +69,12 @@ public void testDayOfWeekNumber() {
6969

7070
@Test
7171
public void testTime() {
72-
assertThat(StrftimeFormatter.format(d, "%X")).isEqualTo("14:22:00");
72+
assertThat(StrftimeFormatter.format(d, "%X")).isEqualTo("2:22:00 PM");
7373
}
7474

7575
@Test
7676
public void testMicrosecs() {
77-
assertThat(StrftimeFormatter.format(d, "%X %f")).isEqualTo("14:22:00 123000");
77+
assertThat(StrftimeFormatter.format(d, "%X %f")).isEqualTo("2:22:00 PM 123000");
7878
}
7979

8080
@Test

0 commit comments

Comments
 (0)