Skip to content

Commit 1764ea8

Browse files
authored
Merge pull request #59 from mursilsayed/mursil/non-optional-exact-milliseconds
DateTime serialization has non optional 3 digit millisecond component
2 parents f770172 + 9572073 commit 1764ea8

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/main/java/graphql/scalars/datetime/DateTimeScalar.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@
1313
import java.time.OffsetDateTime;
1414
import java.time.ZonedDateTime;
1515
import java.time.format.DateTimeFormatter;
16+
import java.time.format.DateTimeFormatterBuilder;
1617
import java.time.format.DateTimeParseException;
1718
import java.util.function.Function;
1819

1920
import static graphql.scalars.util.Kit.typeName;
21+
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
22+
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
23+
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
24+
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
25+
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
2026

2127
/**
2228
* Access this via {@link graphql.scalars.ExtendedScalars#DateTime}
@@ -27,6 +33,7 @@ public final class DateTimeScalar {
2733
public static final GraphQLScalarType INSTANCE;
2834

2935
private DateTimeScalar() {}
36+
private static final DateTimeFormatter customOutputFormatter = getCustomDateTimeFormatter();
3037

3138
static {
3239
Coercing<OffsetDateTime, String> coercing = new Coercing<OffsetDateTime, String>() {
@@ -45,7 +52,7 @@ public String serialize(Object input) throws CoercingSerializeException {
4552
);
4653
}
4754
try {
48-
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetDateTime);
55+
return customOutputFormatter.format(offsetDateTime);
4956
} catch (DateTimeException e) {
5057
throw new CoercingSerializeException(
5158
"Unable to turn TemporalAccessor into OffsetDateTime because of : '" + e.getMessage() + "'."
@@ -102,4 +109,20 @@ private OffsetDateTime parseOffsetDateTime(String s, Function<String, RuntimeExc
102109
.build();
103110
}
104111

112+
private static DateTimeFormatter getCustomDateTimeFormatter() {
113+
return new DateTimeFormatterBuilder()
114+
.parseCaseInsensitive()
115+
.append(ISO_LOCAL_DATE)
116+
.appendLiteral('T')
117+
.appendValue(HOUR_OF_DAY, 2)
118+
.appendLiteral(':')
119+
.appendValue(MINUTE_OF_HOUR, 2)
120+
.appendLiteral(':')
121+
.appendValue(SECOND_OF_MINUTE, 2)
122+
.appendFraction(NANO_OF_SECOND, 3, 3, true)
123+
.appendOffset("+HH:MM", "Z")
124+
.toFormatter();
125+
}
126+
127+
105128
}

src/test/groovy/graphql/scalars/datetime/DateTimeScalarTest.groovy

+11-10
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ class DateTimeScalarTest extends Specification {
4141
result.isEqualTo(expectedValue)
4242
where:
4343
input | expectedValue
44-
"1985-04-12T23:20:50.52Z" | mkStringValue("1985-04-12T23:20:50.52Z")
45-
"1996-12-19T16:39:57-08:00" | mkStringValue("1996-12-19T16:39:57-08:00")
46-
"1937-01-01T12:00:27.87+00:20" | mkStringValue("1937-01-01T12:00:27.87+00:20")
47-
mkOffsetDT(year: 1980, hour: 3) | mkStringValue("1980-08-08T03:10:09+10:00")
48-
mkZonedDT(year: 1980, hour: 3) | mkStringValue("1980-08-08T03:10:09+10:00")
44+
"1985-04-12T23:20:50.52Z" | mkStringValue("1985-04-12T23:20:50.520Z")
45+
"1996-12-19T16:39:57-08:00" | mkStringValue("1996-12-19T16:39:57.000-08:00")
46+
"1937-01-01T12:00:27.87+00:20" | mkStringValue("1937-01-01T12:00:27.870+00:20")
47+
"1937-01-01T12:00+00:20" | mkStringValue("1937-01-01T12:00:00.000+00:20")
48+
mkOffsetDT(year: 1980, hour: 3) | mkStringValue("1980-08-08T03:10:09.000+10:00")
49+
mkZonedDT(year: 1980, hour: 3) | mkStringValue("1980-08-08T03:10:09.000+10:00")
4950
}
5051

5152
@Unroll
@@ -81,11 +82,11 @@ class DateTimeScalarTest extends Specification {
8182
result == expectedValue
8283
where:
8384
input | expectedValue
84-
"1985-04-12T23:20:50.52Z" | "1985-04-12T23:20:50.52Z"
85-
"1996-12-19T16:39:57-08:00" | "1996-12-19T16:39:57-08:00"
86-
"1937-01-01T12:00:27.87+00:20" | "1937-01-01T12:00:27.87+00:20"
87-
mkOffsetDT(year: 1980, hour: 3) | "1980-08-08T03:10:09+10:00"
88-
mkZonedDT(year: 1980, hour: 3) | "1980-08-08T03:10:09+10:00"
85+
"1985-04-12T23:20:50.52Z" | "1985-04-12T23:20:50.520Z"
86+
"1996-12-19T16:39:57-08:00" | "1996-12-19T16:39:57.000-08:00"
87+
"1937-01-01T12:00:27.87+00:20" | "1937-01-01T12:00:27.870+00:20"
88+
mkOffsetDT(year: 1980, hour: 3) | "1980-08-08T03:10:09.000+10:00"
89+
mkZonedDT(year: 1980, hour: 3) | "1980-08-08T03:10:09.000+10:00"
8990
}
9091

9192
def "datetime serialisation bad inputs"() {

0 commit comments

Comments
 (0)