Skip to content

Commit 2d1e9aa

Browse files
Fix datetime serialisation (#16)
Signed-off-by: Graham Campbell <[email protected]>
1 parent addd469 commit 2d1e9aa

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

src/CloudEvents/Serializers/ArraySerializer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
use CloudEvents\Serializers\Exceptions\UnsupportedEventSpecVersionException;
77
use CloudEvents\V1\CloudEventInterface as V1CloudEventInterface;
88
use DateTimeInterface;
9+
use DateTimeZone;
910

1011
use function array_merge;
1112

1213
class ArraySerializer
1314
{
15+
private const DATETIME_FORMAT = 'Y-m-d\TH:i:s\Z';
16+
private const DATETIME_ZONE = 'UTC';
17+
1418
/**
1519
* @throws UnsupportedEventSpecVersionException
1620
* @throws \JsonException
@@ -46,7 +50,9 @@ protected function createPayload(CloudEventInterface $cloudEvent): array
4650

4751
private function formatTime(?DateTimeInterface $time): ?string
4852
{
49-
return $time === null ? null : $time->format(DATE_RFC3339);
53+
return $time === null
54+
? null
55+
: $time->setTimezone(new DateTimeZone(self::DATETIME_ZONE))->format(self::DATETIME_FORMAT);
5056
}
5157

5258
/**

tests/CloudEvents/Serializers/ArraySerializerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function testSerialize(): void
3939
'datacontenttype' => 'application/json',
4040
'dataschema' => 'com.example/schema',
4141
'subject' => 'larger-context',
42-
'time' => '2018-04-05T17:31:00+00:00',
42+
'time' => '2018-04-05T17:31:00Z',
4343
'data' => [
4444
'key' => 'value',
4545
]

tests/CloudEvents/Serializers/JsonSerializerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testSerialize(): void
4040
'datacontenttype' => 'application/json',
4141
'dataschema' => 'com.example/schema',
4242
'subject' => 'larger-context',
43-
'time' => '2018-04-05T17:31:00+00:00',
43+
'time' => '2018-04-05T17:31:00Z',
4444
'data' => [
4545
'key' => 'value',
4646
]

0 commit comments

Comments
 (0)