Saturate From<SystemTime> for DateTime<Utc> instead of panicking - #1811
Open
n0liu wants to merge 1 commit into
Open
Saturate From<SystemTime> for DateTime<Utc> instead of panicking#1811n0liu wants to merge 1 commit into
n0liu wants to merge 1 commit into
Conversation
`From<SystemTime> for DateTime<Utc>` is infallible at the type level but unwrapped `Utc.timestamp_opt(...)`, so a `SystemTime` outside the range of `DateTime` panicked. Saturate to `MAX_UTC` / `MIN_UTC` for out-of-range inputs instead; normal values are unaffected. Addresses the `From<SystemTime>` finding in chronotope#1802.
djc
approved these changes
Aug 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses the
From<SystemTime>finding in #1802.From<SystemTime> for DateTime<Utc>is infallible at the type level, but it unwrapsUtc.timestamp_opt(...), so aSystemTimeoutside the range ofDateTimepanics:Since the conversion cannot return an error, this saturates to
DateTime::<Utc>::MAX_UTC/MIN_UTCfor out-of-range inputs instead of panicking. Normal values are unaffected.If you would rather document the panic (or make the conversion fallible) than saturate, I am happy to change direction.
Added assertions to
test_from_system_time. Full test suite,cargo fmt,clippy -D warnings, and--no-default-featuresall pass.