chrono icon indicating copy to clipboard operation
chrono copied to clipboard

Err(ParseError(TooShort)) when parsing datetime with trailing 0

Open MarcoGorelli opened this issue 1 year ago • 4 comments

This was originally reported here: https://github.com/pola-rs/polars/issues/17167

To reproduce:

[package]
name = "scratch"
version = "0.1.0"
edition = "2021"

[dependencies]
chrono = "0.4.38"

src/main.rs

use chrono::NaiveDateTime;
fn main() {

    let result = NaiveDateTime::parse_from_str(
        "2024-06-03 20:02:48.6800000",
        "%Y-%m-%d %H:%M:%S%.6f0",
    );
    println!("{:?}", result);
    let result = NaiveDateTime::parse_from_str(
        "2024-06-03 20:02:48.680000",
        "%Y-%m-%d %H:%M:%S%.6f",
    );
    println!("{:?}", result);
}

prints

Err(ParseError(TooShort))
Ok(2024-06-03T20:02:48.680)

MarcoGorelli avatar Jun 25 '24 12:06 MarcoGorelli

What result would you expect?

djc avatar Jun 25 '24 12:06 djc

probably Ok(2024-06-03T20:02:48.680), which is similar to what Python stdlib would do

>>> datetime.strptime("2024-06-03 20:02:48.6800000", "%Y-%m-%d %H:%M:%S.%f0")
datetime.datetime(2024, 6, 3, 20, 2, 48, 680000)

the workaround on the user's side is very easy (just strip the trailing '0'), so this can easily just be considered out-of-scope if it adds unnecessary complexity here

MarcoGorelli avatar Jun 25 '24 12:06 MarcoGorelli

Right. I think we should probably fix it -- would you be able to submit a PR?

djc avatar Jun 26 '24 08:06 djc

I can give it a go, thanks!

MarcoGorelli avatar Jun 26 '24 08:06 MarcoGorelli