Reputation: 451
I am trying to convert a string to a date using the Chrono library. I always get a BadFormat
or NotEnough
error:
extern crate chrono;
use chrono::prelude::*;
fn main() {
let dt1 = DateTime::parse_from_str(
"2017-08-30 18:34:06.638997932 UTC",
"%Y-%m-%d %H:%M:%S.%9f"
);
println!("{:?}", dt1);
}
I'm not sure what I am doing wrong.
Upvotes: 0
Views: 1010
Reputation: 432039
As mentioned in the comments, your format string doesn't allow for the " UTC"
part of your string. That's why you get the BadFormat
error.
If you add " UTC"
to your format string, you still get a BadFormat
error because you've typed .%9f
when it should be %.9f
.
Once you fix that, you get a NotEnough
error because we aren't actually parsing a timezone.
I'd use NaiveDateTime
to always parse in UTC and then add the " UTC"
onto the format string to ignore it, correcting the typo:
use chrono::prelude::*; // 0.4.9
fn main() {
let dt1 = NaiveDateTime::parse_from_str(
"2017-08-30 18:34:06.638997932 UTC",
"%Y-%m-%d %H:%M:%S%.9f UTC",
);
println!("{:?}", dt1); // Ok(2017-08-30T18:34:06.638997932)
}
Upvotes: 2