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

Add DateTime::to_timezone and FromOffset trait #1569

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pitdicker
Copy link
Collaborator

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.

Copy link

codecov bot commented Apr 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@djc
Copy link
Member

djc commented Apr 12, 2024

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
Copy link
Collaborator Author

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

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.

2 participants