Skip to content

Commit a09b03b

Browse files
authored
Serialize time to RFC3339 compliant string (#249)
Signed-off-by: Zhixuan Lai <zhixuan@squareup.com> Co-authored-by: Zhixuan Lai <zhixuan@squareup.com>
1 parent f219b69 commit a09b03b

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

api/src/main/java/io/cloudevents/types/Time.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.time.OffsetDateTime;
2121
import java.time.format.DateTimeParseException;
2222

23+
import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME;
24+
2325
/**
2426
* Utilities to handle the <a href="https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system">CloudEvent Attribute Timestamp type</a>
2527
*/
@@ -36,9 +38,9 @@ public static OffsetDateTime parseTime(String time) throws DateTimeParseExceptio
3638
}
3739

3840
/**
39-
* Convert a {@link OffsetDateTime} to {@link String}
41+
* Convert a {@link OffsetDateTime} to a RFC3339 compliant {@link String}
4042
*/
4143
public static String writeTime(OffsetDateTime time) throws DateTimeParseException {
42-
return time.toString();
44+
return ISO_OFFSET_DATE_TIME.format(time);
4345
}
4446
}

api/src/test/java/io/cloudevents/types/TimeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void testSerializeDateOffset() {
4949
assertThat(Time.writeTime(OffsetDateTime.of(
5050
LocalDateTime.of(2020, 8, 3, 18, 10, 0, 0),
5151
ZoneOffset.ofHours(2)
52-
))).isEqualTo("2020-08-03T18:10+02:00");
52+
))).isEqualTo("2020-08-03T18:10:00+02:00");
5353
}
5454

5555
public static Stream<Arguments> parseDateArguments() {

formats/json-jackson/src/test/resources/v1/json_data_with_fractional_time.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
"datacontenttype": "application/json",
88
"data": {},
99
"subject": "sub",
10-
"time": "2018-04-26T14:48:09.123400Z"
10+
"time": "2018-04-26T14:48:09.1234Z"
1111
}

0 commit comments

Comments
 (0)