Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datetime.timedelta Support #475

Merged
merged 4 commits into from
Jul 11, 2023
Merged

datetime.timedelta Support #475

merged 4 commits into from
Jul 11, 2023

Conversation

jcrist
Copy link
Owner

@jcrist jcrist commented Jul 8, 2023

This adds builtin timedelta support.

By default, we encode-to/decode-from a subset of ISO 8601 duration strings.

This subset matches the intersection between the one used by java's duration, and the proposed javascript Temporal.Duration api (docs here). Duration strings produced by msgspec should be compatible with either of these libraries (and most other ISO 8601 compatible systems). Likewise, durations produced by those libraries should be readable by msgspec. See the included docs update for a complete spec of what ISO8601 subset is supported.

  • A remaining TODO is adding support for parsing int/float inputs as timedeltas (interpreted as total seconds) when in lax-mode (strict=False).

We might want to also add a timedelta_format option to the encoders so users can also encode timedelta objects in this format as well. I'm less certain this is necessary. A double can only represent a timedelta of ~270 years down to microsecond precision, deltas larger than this will start to lose precision when serialized as floats. Whether that matters is use case dependent.

Fixes #260.

These are encoded to ISO8601 duration strings. Only `'D'`, `'H'`, `'M'`,
and `'S'` units are supported (no relative units like `Y` or `M`
(month)).
If `strict=False` we also support decoding int/float values (or str
representations of these values) to `timedelta` types. Note that a
`double` cannot represent a timedelta with microsecond precision if
the value is > ~270 years. For true roundtrip support we recommend
serializing timedeltas as ISO 8601 strings.
@jcrist jcrist changed the title [WIP] datetime.timedelta Support datetime.timedelta Support Jul 11, 2023
@jcrist jcrist merged commit 8d70fc0 into main Jul 11, 2023
7 checks passed
@jcrist jcrist deleted the parse-iso8601-duration branch July 11, 2023 02:24
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.

Support serialization of datetime.timedelta
1 participant