Search code examples
datetimerustdatetime-formatrust-chrono

Rust chrono gives ParseError(NotEnough)


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");


Solution

  • 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)