Serialize time to RFC3339 compliant string (#249)

Signed-off-by: Zhixuan Lai <zhixuan@squareup.com>

Co-authored-by: Zhixuan Lai <zhixuan@squareup.com>
This commit is contained in:
Zhixuan Lai 2020-09-30 04:03:13 -07:00 committed by GitHub
parent f219b6937c
commit a09b03bd2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 4 deletions

View File

@ -20,6 +20,8 @@ package io.cloudevents.types;
import java.time.OffsetDateTime;
import java.time.format.DateTimeParseException;
import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME;
/**
* Utilities to handle the <a href="https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system">CloudEvent Attribute Timestamp type</a>
*/
@ -36,9 +38,9 @@ public final class Time {
}
/**
* Convert a {@link OffsetDateTime} to {@link String}
* Convert a {@link OffsetDateTime} to a RFC3339 compliant {@link String}
*/
public static String writeTime(OffsetDateTime time) throws DateTimeParseException {
return time.toString();
return ISO_OFFSET_DATE_TIME.format(time);
}
}

View File

@ -49,7 +49,7 @@ public class TimeTest {
assertThat(Time.writeTime(OffsetDateTime.of(
LocalDateTime.of(2020, 8, 3, 18, 10, 0, 0),
ZoneOffset.ofHours(2)
))).isEqualTo("2020-08-03T18:10+02:00");
))).isEqualTo("2020-08-03T18:10:00+02:00");
}
public static Stream<Arguments> parseDateArguments() {

View File

@ -7,5 +7,5 @@
"datacontenttype": "application/json",
"data": {},
"subject": "sub",
"time": "2018-04-26T14:48:09.123400Z"
"time": "2018-04-26T14:48:09.1234Z"
}