chronotope / chrono

Date and time library for Rust
Other
3.3k stars 523 forks source link

Add `DateTime::to_timezone` and `FromOffset` trait #1569

Open pitdicker opened 5 months ago

pitdicker commented 5 months ago

We can't have a generic From or Into implementation that converts from one Offset type into another because the standard library has a blanket implementation of From<T> for T. For the same reason we can't have a generic From or Into implementation that converts from one DateTime<Tz> into another.

So we can and do have implementations to convert between DateTime<Utc>, DateTime<FixedOffset> and DateTime<Local>. But any TimeZone type outside chrono doesn't have this convenience, such as the one in chrono-tz.

I propose to add a FromOffset trait that Offset types can implement to convert from any generic Offset. And to add a DateTime::to_timezone method to conveniently convert a DateTime<Tz> into another type.

If we implement FromOffset in chrono-tz it we can do nice things such as converting from FixedOffset to a Tz time zone for a fixed offset, or eventually from LocalOffset (#1563) to a Tz with the same time zone if possible.

codecov[bot] commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 91.83%. Comparing base (f8cecbe) to head (2a8af8b). Report is 17 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1569 +/- ## ========================================== + Coverage 91.80% 91.83% +0.02% ========================================== Files 37 37 Lines 18151 18198 +47 ========================================== + Hits 16664 16712 +48 + Misses 1487 1486 -1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

djc commented 5 months ago

I propose to add a FromOffset trait that Offset types can implement to convert from any generic Offset. And to add a DateTime::to_timezone method to conveniently convert a DateTime<Tz> into another type.

If we implement FromOffset in chrono-tz it we can do nice things such as converting from FixedOffset to a Tz time zone for a fixed offset, or eventually from LocalOffset (#1563) to a Tz with the same time zone if possible.

This all sounds kind of speculative and not as a current/near-future thing?

If so, I'd rather hold off on merging this now.

Alternatively stated: what do you envision to be the concrete near-term benefit for our users?

pitdicker commented 5 months ago

I hope to prove it useful soon. Fine to hold off for now.