3

I am trying to parse a datetime string to DateTime object, but when I try this I am getting this ParseError. I don't understand what is going on, can someone help me out?

datetime string: 09-January-2018 12:00:00

code: let date = DateTime::parse_from_str(date.trim(), "%d-%B-%Y %T");

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Catman155
  • 155
  • 1
  • 9

1 Answers1

9

This:

extern crate chrono;
use chrono::DateTime;
use std::error::Error;

fn main() {
    println!("{:?}", DateTime::parse_from_str("09-January-2018 12:00:00", "%d-%B-%Y %T").unwrap_err().description());
}

(https://play.rust-lang.org/?gist=9c0231ea189c589009a46308864dd9bc&version=stable)

gives more information:

"input is not enough for unique date and time"

Apparently, DateTime needs Timezone information, which you don't provide in your input. Using NaiveDateTime should work:

extern crate chrono;
use chrono::NaiveDateTime;

fn main() {
    println!("{:?}", NaiveDateTime::parse_from_str("09-January-2018 12:00:00", "%d-%B-%Y %T"));
}

(https://play.rust-lang.org/?gist=1acbae616c7f084a748e4f9cfaf1ef7f&version=stable)

musicmatze
  • 4,124
  • 7
  • 33
  • 48