chrono
chrono copied to clipboard
Err(ParseError(TooShort)) when parsing datetime with trailing 0
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)
What result would you expect?
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
Right. I think we should probably fix it -- would you be able to submit a PR?
I can give it a go, thanks!