Skip to content

Add TimeMode encoding option TimeRFC3339NanoUTC#688

Merged
fxamacker merged 1 commit intomasterfrom
fxamacker/add-TimeRFC3339NanoUTC-option
Jul 20, 2025
Merged

Add TimeMode encoding option TimeRFC3339NanoUTC#688
fxamacker merged 1 commit intomasterfrom
fxamacker/add-TimeRFC3339NanoUTC-option

Conversation

@fxamacker
Copy link
Copy Markdown
Owner

@fxamacker fxamacker commented Jul 20, 2025

Closes #687

This PR adds TimeRFC3339NanoUTC option to encode time.Time to CBOR time (tag 0) with a text string content representing UTC time with nanosecond precision in RFC3339 format.

While at it, also refactored some existing tests and added more tests.

NOTE: The TimeUnix option (default setting) uses 1-second precision, UTC, and encodes to integer (CBOR tag 1). It is the recommended setting unless you need fractional seconds.

If user apps need to encode UTC time with nanosecond precision to integer (instead of string), then that is supported by this library in the API for user-defined CBOR tags.

Thanks @ssanderson for suggesting this feature (TimeRFC3339NanoUTC) at:

Context

RFC 8949 defines CBOR tag 0 (time encoded to string value) and CBOR tag 1 (time encoded to integer or floating point).

Unfortunately, RFC 8949 does not define an integer representation of time with sub-second precision.

To comply with RFC 8949 for CBOR tag 1, the TimeUnixMicro option encodes POSIX [TIME_T] in UTC to floating point (instead of encoding to integer).

Since floating point is too lossy for some use cases, this PR added another option for CBOR tag 0 (time encoded to string value).

This commit adds TimeMode encoding option TimeRFC3339NanoUTC which
causes time.Time to encode to a CBOR time (tag 0) with a text string
content representing UTC time using nanosecond precision in RFC3339
format.
@fxamacker fxamacker self-assigned this Jul 20, 2025
@fxamacker fxamacker added the enhancement New feature or request label Jul 20, 2025
@fxamacker
Copy link
Copy Markdown
Owner Author

@x448 PTAL

Copy link
Copy Markdown
Contributor

@x448 x448 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@fxamacker fxamacker merged commit cc509f3 into master Jul 20, 2025
22 checks passed
@fxamacker fxamacker deleted the fxamacker/add-TimeRFC3339NanoUTC-option branch September 16, 2025 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature: Add TimeRFC3339NanoUTC option for encoding to CBOR time (tag 0)

2 participants