time-rs / time

The most used Rust library for date and time handling.
https://time-rs.github.io
Apache License 2.0
1.1k stars 277 forks source link

Fallible to_offset #586

Closed SvizelPritula closed 1 year ago

SvizelPritula commented 1 year ago

The to_offset function can panic if timezone conversion shifts a date outside of the valid range. As dates are often sourced from outside sources and programs should avoid panics, it would be useful to have a checked variant that returns an option.

Currently the only option is to perform the conversion manually by turning the OffsetDateTime into a PrimitiveDateTime, the Offsets through seconds into a Duration and using the checked_sub and checked_add methods before reassembling an OffsetDateTime.

While I no longer personally have a use case for to_offset, I originally planed to use it to convert parsed dates into UTC to allow for faster comparisons, although I suppose that if comparison speed ever truly becomes an issue I should just switch to timestamps instead. Nevertheless, I still think a fallible version of to_offset could be useful in the future.

jhpratt commented 1 year ago

Added OffsetDateTime::checked_to_offset. Waiting on CI before releasing.