Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ParseError(NotEnough) from rust-chrono mean?

I'm using rust-chrono and I'm trying to parse a date like this:

extern crate chrono;

use chrono::*;

fn main() {

    let date_str = "2013-02-14 15:41:07";
    let date = DateTime::parse_from_str(&date_str, "%Y-%m-%d %H:%M:%S");
    match date {
        Ok(v) => println!("{:?}", v),
        Err(e) => println!("{:?}", e)
    }

}

And this is the output:

ParseError(NotEnough)

What does this mean? Not enough of what? Should I be using some other library?

like image 758
Caballero Avatar asked May 24 '16 15:05

Caballero


2 Answers

Types that implement Error have more user-friendly error messages via Error::description or Display:

Err(e) => println!("{}", e)

This prints:

input is not enough for unique date and time

Presumably this is because you haven't provided a timezone, thus the time is ambiguous.

like image 171
Shepmaster Avatar answered Sep 21 '22 17:09

Shepmaster


The ParseError(NotEnough) shows up when there is not enough information to fill out the whole object. For example the date, time or timezone is missing.

In the example above the timezone is missing. So we can store it in a NaiveDateTime. This object does not store a timezone:

let naive_datetime = NaiveDateTime::parse_from_str(date_str, "%Y-%m-%d %H:%M:%S").unwrap();

For more info: https://stackoverflow.com/a/61179071/2037998

like image 25
Ralph Bisschops Avatar answered Sep 19 '22 17:09

Ralph Bisschops