Skip to content

Enable configuration of sub-second precision of timestamps#54

Merged
GrahamCampbell merged 1 commit intocloudevents:mainfrom
GrahamCampbell:configurable-subsecond-precision
Dec 18, 2023
Merged

Enable configuration of sub-second precision of timestamps#54
GrahamCampbell merged 1 commit intocloudevents:mainfrom
GrahamCampbell:configurable-subsecond-precision

Conversation

@GrahamCampbell
Copy link
Copy Markdown
Contributor

@GrahamCampbell GrahamCampbell commented Dec 14, 2023

This PR enables the configuration of the sub-second precision in serialized timestamps. None of these changes are breaking because either the changes are made to classes marked as internal or a new constructor added to a final class with all parameters optional, and the default precision remains unchanged.

Example usage, serializing a cloudevent timestamp including the milliseconds but not the microseconds:

$cloudEvent = (new CloudEvents\V1\CloudEventImmutable('a', 'b', 'c'))
    ->withTime(new DateTimeImmutable('2023-01-01T01:23:45.567890Z'));

$serializer = new CloudEvents\Serializers\JsonSerializer(
    new CloudEvents\Serializers\Normalizers\V1\Normalizer([
        'subsecondPrecision' => 3,
    ]),
);

// {"specversion":"1.0","id":"a","source":"b","type":"c","time":"2023-01-01T01:23:45.567Z"}
echo $serializer->serializeStructured($cloudEvent);

Note that on the deserialisation side of the house, we already can handle sub-second timestamps of any precision, truncating down to at most 6 digits, as supported by PHP, without any configuration.

Signed-off-by: Graham Campbell <hello@gjcampbell.co.uk>
@jlaswell jlaswell self-requested a review December 14, 2023 12:58
@GrahamCampbell GrahamCampbell merged commit 45c5e1b into cloudevents:main Dec 18, 2023
@GrahamCampbell GrahamCampbell deleted the configurable-subsecond-precision branch December 18, 2023 02:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants