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

Convience method for converting from DateTime<FixedOffset> to UTC #169

Closed
glennpierce opened this issue Jun 29, 2017 · 6 comments
Closed

Comments

@glennpierce
Copy link

Hi

Currently I am doing something like

let dt : DateTime = DateTime::parse_from_rfc3339("1970-01-01T00:00:00+00:00")?;
let dt_utc : DateTime = DateTime::::from_utc(dt.naive_utc(), UTC);

Is there any more convenient way todo this and vice versa ?

Thanks

@quodlibetor
Copy link
Contributor

The fact that Utc is a type (and therefore can't depend on anything at run-time) means that it's impossible for methods on DateTime<FixedOffset> (which is where the parse_from_rfc3339 is implemented) to parse to a DateTime<Utc>.

However, we could probably implement either a DateTime<Utc>::parse_from_rfc3339() (or probably nicer a Utc::parse_from_rfc3339()) that does the parsing and auto-timezone conversion.

Doing that depends a bit on the philosophical outlook of this trait: whether we want to implement a whole bunch of kind-of-trivial ergonomic methods or if we want to keep the API surface small. We're currently thinking about the tradeoffs of the two philosophies.

@glennpierce
Copy link
Author

glennpierce commented Jun 30, 2017

Thanks

I can understand wanting to keep the api simple I guess. However, speaking from my experience from Python I would expect other crates that wrap chrono to popup eventually and I am not sure that is desirable.

The Python datetime library was difficult to work with and that lead to the "dateutil", "arrow", and "simple-date" wrapper libraries and probably more I don't know about.

@lilith
Copy link

lilith commented Sep 24, 2017

I've spent about 30 minutes trying to figure out how to correctly convert between DateTime<Utc> and DateTime<FixedOffset>. I read the entire API documentation without enlightenment.

I support making that conversion obvious.

.with_timezone(&FixedOffset::east(0)) (UTC->Fixed) wasn't obvious. FixedOffset::zero() or ::utc() would have helped.

@Walther
Copy link

Walther commented Nov 16, 2018

If a convenience method (e.g. FixedOffset::now()) would not be feasible, maybe the documentation could be improved? On a quick reading, the Readme seems to warmly suggest using FixedOffset as "it stores the most information and does not depend on the system environment". However, there's no instruction / example on how to properly use it.

EDIT: oops, misread the original issue, sorry. This comment is kind of tangential / unrelated.

@epipheus
Copy link

I also have to google around a long time and lucked upon finding this issue.

@djc
Copy link
Member

djc commented Sep 26, 2022

I'm going to close this issue. The original request has been addressed with #806 in that you can now call parse_from_rfc3339() on DateTime<Utc>. Additionally, an impl From<DateTime<FixedOffset>> for DateTime<Utc> already exists.

However, I'm definitely open to the idea that ergonomic issues still exist here! In order to address those, please open a new issue with some example code of where you're running into trouble/a conversion that seems to require more complex code than what you feel is necessary.

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

No branches or pull requests

6 participants