Skip to content

Commit a0e6e19

Browse files
committed
Add support for ISO date/times.
1 parent d1cbada commit a0e6e19

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

httprpc-server/src/main/java/org/httprpc/io/TemplateEncoder.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.nio.charset.Charset;
3030
import java.nio.charset.StandardCharsets;
3131
import java.text.NumberFormat;
32+
import java.time.ZonedDateTime;
3233
import java.time.format.DateTimeFormatter;
3334
import java.time.format.FormatStyle;
3435
import java.time.temporal.TemporalAccessor;
@@ -85,16 +86,19 @@ private static class FormatModifier implements Modifier {
8586
static final String MEDIUM_DATE = "mediumDate";
8687
static final String LONG_DATE = "longDate";
8788
static final String FULL_DATE = "fullDate";
89+
static final String ISO_DATE = "isoDate";
8890

8991
static final String SHORT_TIME = "shortTime";
9092
static final String MEDIUM_TIME = "mediumTime";
9193
static final String LONG_TIME = "longTime";
9294
static final String FULL_TIME = "fullTime";
95+
static final String ISO_TIME = "isoTime";
9396

9497
static final String SHORT_DATE_TIME = "shortDateTime";
9598
static final String MEDIUM_DATE_TIME = "mediumDateTime";
9699
static final String LONG_DATE_TIME = "longDateTime";
97100
static final String FULL_DATE_TIME = "fullDateTime";
101+
static final String ISO_DATE_TIME = "isoDateTime";
98102

99103
enum DateTimeType {
100104
DATE,
@@ -133,6 +137,10 @@ public Object apply(Object value, String argument, Locale locale, TimeZone timeZ
133137
return format(value, DateTimeType.DATE, FormatStyle.FULL, locale, timeZone);
134138
}
135139

140+
case ISO_DATE: {
141+
return format(value, DateTimeType.DATE, null, null, timeZone);
142+
}
143+
136144
case SHORT_TIME: {
137145
return format(value, DateTimeType.TIME, FormatStyle.SHORT, locale, timeZone);
138146
}
@@ -149,6 +157,10 @@ public Object apply(Object value, String argument, Locale locale, TimeZone timeZ
149157
return format(value, DateTimeType.TIME, FormatStyle.FULL, locale, timeZone);
150158
}
151159

160+
case ISO_TIME: {
161+
return format(value, DateTimeType.TIME, null, null, timeZone);
162+
}
163+
152164
case SHORT_DATE_TIME: {
153165
return format(value, DateTimeType.DATE_TIME, FormatStyle.SHORT, locale, timeZone);
154166
}
@@ -165,6 +177,10 @@ public Object apply(Object value, String argument, Locale locale, TimeZone timeZ
165177
return format(value, DateTimeType.DATE_TIME, FormatStyle.FULL, locale, timeZone);
166178
}
167179

180+
case ISO_DATE_TIME: {
181+
return format(value, DateTimeType.DATE_TIME, null, null, timeZone);
182+
}
183+
168184
default: {
169185
return String.format(locale, argument, value);
170186
}
@@ -177,22 +193,34 @@ static String format(Object value, DateTimeType dateTimeType, FormatStyle format
177193
}
178194

179195
if (value instanceof Date) {
180-
value = ((Date)value).toInstant().atZone(timeZone.toZoneId()).toLocalDateTime();
196+
value = ZonedDateTime.ofInstant(((Date)value).toInstant(), timeZone.toZoneId());
181197
}
182198

183199
TemporalAccessor temporalAccessor = (TemporalAccessor)value;
184200

185201
switch (dateTimeType) {
186202
case DATE: {
187-
return DateTimeFormatter.ofLocalizedDate(formatStyle).withLocale(locale).format(temporalAccessor);
203+
if (formatStyle != null) {
204+
return DateTimeFormatter.ofLocalizedDate(formatStyle).withLocale(locale).format(temporalAccessor);
205+
} else {
206+
return DateTimeFormatter.ISO_OFFSET_DATE.format(ZonedDateTime.from(temporalAccessor));
207+
}
188208
}
189209

190210
case TIME: {
191-
return DateTimeFormatter.ofLocalizedTime(formatStyle).withLocale(locale).format(temporalAccessor);
211+
if (formatStyle != null) {
212+
return DateTimeFormatter.ofLocalizedTime(formatStyle).withLocale(locale).format(temporalAccessor);
213+
} else {
214+
return DateTimeFormatter.ISO_OFFSET_TIME.format(ZonedDateTime.from(temporalAccessor));
215+
}
192216
}
193217

194218
case DATE_TIME: {
195-
return DateTimeFormatter.ofLocalizedDateTime(formatStyle).withLocale(locale).format(temporalAccessor);
219+
if (formatStyle != null) {
220+
return DateTimeFormatter.ofLocalizedDateTime(formatStyle).withLocale(locale).format(temporalAccessor);
221+
} else {
222+
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.from(temporalAccessor));
223+
}
196224
}
197225

198226
default: {

httprpc-server/src/test/java/org/httprpc/io/TemplateEncoderTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ public void testDateFormatModifiers() throws IOException {
276276
ZonedDateTime now = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
277277

278278
assertEquals(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(now) + ",\n"
279+
+ DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(now) + ",\n"
279280
+ DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(now) + ",\n"
281+
+ DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(now) + ",\n"
280282
+ DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(localDate) + ",\n"
281283
+ DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).format(localTime) + ",\n"
282284
+ DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).format(localDateTime), result);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{{timestamp:format=shortDate}},
2+
{{timestamp:format=isoDateTime}},
23
{{date:format=shortDate}},
4+
{{date:format=isoDateTime}},
35
{{localDate:format=shortDate}},
46
{{localTime:format=shortTime}},
57
{{localDateTime:format=shortDateTime}}

template-reference.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,17 @@ In addition to `printf()`-style formatting, the `format` modifier also supports
6262
* `mediumDate` - applies a medium date format
6363
* `longDate` - applies a long date format
6464
* `fullDate` - applies a full date format
65+
* `isoDate` - applies an ISO date format
6566
* `shortTime` - applies a short time format
6667
* `mediumTime` - applies a medium time format
6768
* `longTime` - applies a long time format
6869
* `fullTime` - applies a full time format
70+
* `isoTime` - applies an ISO time format
6971
* `shortDateTime` - applies a short date/time format
7072
* `mediumDateTime` - applies a medium date/time format
7173
* `longDateTime` - applies a long date/time format
7274
* `fullDateTime` - applies a full date/time format
75+
* `isoDateTime` - applies an ISO date/time format
7376

7477
For example, this marker transforms a date value into a medium-length, localized date string:
7578

0 commit comments

Comments
 (0)