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

confusing deprecation notice for chrono::TimeZone::datetime_from_str #1381

Closed
decathorpe opened this issue Jan 23, 2024 · 7 comments
Closed

Comments

@decathorpe
Copy link

I'm regularly using something like Utc.datetime_from_str(foo, "%Y-%m-%d %H:%M") in my projects to parse timestamps into a Datetime<Utc>, and I'm now getting deprecation warnings for that.

I tried replacing those calls with DateTime::parse_from_str as it's documented in the deprecation warning - but that returns a Datetime<FixedOffset>, and I do not see a way to convert a Datetime<FixedOffset> into a Datetime<Utc> (at least no obvious ones), so it is not a direct replacement. Reading the documentation, I'm also unsure whether it's even semantically equivalent.

Changing the type of any parsed timestamps to Datetime<FixedOffset> everywhere would be a pretty tedious ... is there a way to convert a Datetime<FixedOffset> to a Datetime<Utc> to get back the previous return type?

@jtmoon79
Copy link
Contributor

convert a Datetime<FixedOffset> to a Datetime<Utc>

Can you use the From trait?

    let dtfo: DateTime<FixedOffset> = DateTime::parse_from_str("19700102T23:55:55+00", "%Y%m%dT%H:%M:%S%#z").unwrap();
    let dtutc: DateTime<Utc> = DateTime::<Utc>::from(dtfo);

@decathorpe
Copy link
Author

hum ... from the docs for DateTime::parse_from_str:

Note that this method requires a timezone in the input string.

My input string does not have a timezone, but I know that it's a UTC timestamp.

So should I use a NaiveDateTime? But From<NaiveDateTime> is not implemented for DateTime<Utc>.

@decathorpe
Copy link
Author

Indeed, using Datetime::parse_from_str is not possible with the format I have, I get: "input is not enough for unique date and time"

@pitdicker
Copy link
Collaborator

You probably want to parse the input as a NaiveDateTime. That is the closest type to your input, because the timezone information ('UTC') is not part of the input but something you add to it. Code:

NaiveDateTime::parse_from_str(foo, "%Y-%m-%d %H:%M").unwrap().and_utc();

@decathorpe
Copy link
Author

Yup, this works, thank you!

Maybe the deprecation warning could mention both DateTime::parse_from_str and NaiveDateTime::parse_from_str then? It looks like the former is only a replacement if the format has specified a timezone, and the latter needs to be used if the timezone is not part of the format.

@pitdicker
Copy link
Collaborator

It does since 0.4.32, released two days ago (see #1342) 😄.

@decathorpe
Copy link
Author

Ah! Thank you. I probably did not notice because 0.4.32 failed to build on docs.rs.

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

3 participants