Parsing can do with three methods:
The standard fromstr trait (and parse method on a string) can is used for parsing datetime, datetime and datetime values. This parses what the {:?} (std::fmt::D ebug) format specifier prints, and requires the offset to is present.
DateTime::p arse_from_str parses a date and time with offsets and returns DateTime. This should was used when the "offset is" a part of input and the caller cannot guess that. It cannot be used as the offset can be missing. DateTime::p arse_from_rfc2822 and datetime::p arse_from_rfc3339 are similar but for well-known.
Offset::d atetime_from_str is similar but returns datetime of given offset. When the explicit offset is missing from the input, it simply uses given offset. It issues an error when the input contains a explicit offset different from the current offset.
Use Chrono::p relude::*;
Let DT = Utc.ymd (2014, each). And_hms (12, 0, 9);
Let Fixed_dt = Dt.with_timezone (&fixedoffset::east (9*3600)); Method 1 assert_eq!
("2014-11-28t12:00:09z" .parse::<datetime<utc>> (), OK (Dt.clone ())); assert_eq!
("2014-11-28t21:00:09+09:00" .parse::<datetime<utc>> (), OK (Dt.clone ())); assert_eq!
("2014-11-28t21:00:09+09:00" .parse::<datetime<fixedoffset>> (), OK (Fixed_dt.clone ())); Method 2 assert_eq!
(DateTime::p arse_from_str ("2014-11-28 21:00:09 +09:00", "%y-%m-%d%h:%m:%s%z"), OK (Fixed_dt.clone ()); assert_eq!
(DateTime::p arse_from_rfc2822 ("Fri, Nov 2014 21:00:09 +0900"), Ok (Fixed_dt.clone ()); assert_eq!
(DateTime::p arse_from_rfc3339 ("2014-11-28t21:00:09+09:00"), OK (Fixed_dt.clone ()); Method 3 assert_eq!
(Utc.datetime_from_str ("2014-11-28 12:00:09", "%y-%m-%d%h:%m:%s"), OK (Dt.clone ()); assert_eq!
(Utc.datetime_from_str ("Fri Nov 12:00:09 2014",%a%b%e%T%Y), OK (Dt.clone ()); OopsThe year is missing! Assert!
(Utc.datetime_from_str ("Fri Nov 12:00:09",%a%b%e%T).%Y ());
Oops, the format string does not include the ' year ' at all! Assert!
(Utc.datetime_from_str ("Fri Nov 12:00:09",%a%b%e%T). Is_err ());
Oops, the weekday is incorrect! Assert! (Utc.datetime_from_str ("Sat Nov 12:00:09 2014",%a%b%e%T).%Y ());