Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MarcoGorelli opened this issue Jun 25, 2024 · 4 comments
Open

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

MarcoGorelli opened this issue Jun 25, 2024 · 4 comments

Comments

@MarcoGorelli
Copy link
Contributor

This was originally reported here: pola-rs/polars#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)
@djc
Copy link
Contributor

djc commented Jun 25, 2024

What result would you expect?

@MarcoGorelli
Copy link
Contributor Author

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

@djc
Copy link
Contributor

djc commented Jun 26, 2024

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

@MarcoGorelli
Copy link
Contributor Author

I can give it a go, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants