chrono icon indicating copy to clipboard operation
chrono copied to clipboard

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

Open pitdicker opened this issue 1 year ago • 3 comments
trafficstars

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.

pitdicker avatar Apr 12 '24 07:04 pitdicker

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
@@            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.

codecov[bot] avatar Apr 12 '24 07:04 codecov[bot]

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?

djc avatar Apr 12 '24 07:04 djc

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

pitdicker avatar Apr 12 '24 08:04 pitdicker