Chrono: Create local time from Y, m, d, H, M

I want to make a DateTime from year, month, day, hour, minute in local time.

I am using the following statement (which is naive as it has no time zone)
DateTime::parse_from_str("2020 01 09 23 54", " %Y %m %d %H %M");

I am getting: ParseError(NotEnough). I am puzzled. I expected a result in UTC

The documentation says to use Offset::datetime_from_str It lost me there. How should I do this?

Since it's naive, you need to use NaiveDateTime::parse_from_str - DateTime::parse_from_str requires a timezone in the format string. That'll get you a NaiveDateTime; if you want to get a DateTime from that in UTC, you can do DateTime::<Utc>::from_utc(parsed, chrono::Utc).

2 Likes

Thank you.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.