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

impossible to parse Unix timestamp with subsecond precision. #175

Closed
jonasbb opened this issue Jul 13, 2017 · 1 comment
Closed

impossible to parse Unix timestamp with subsecond precision. #175

jonasbb opened this issue Jul 13, 2017 · 1 comment

Comments

@jonasbb
Copy link
Contributor

jonasbb commented Jul 13, 2017

I need to parse a Unix timestamp with subsecond precision, like 1441497364.649. This should equal the format string %s%.3f. I am only able to parse such a string, if the subsecond part is equal to 0. Otherwise I only get an Impossible error, which I don't understand, because there are not inconsistencies in the timestamp.

I tested it with this program

fn main() {
    let fmt = "%s%.3f";

    let dt = NaiveDate::from_ymd(2015, 9, 5).and_hms_milli(23, 56, 4, 649);
    println!("{}", dt.format(fmt).to_string());
    let s = dt.format(fmt).to_string();
    let dt2 = NaiveDateTime::parse_from_str(&s, fmt);
    println!("{:?}", dt2);
    if let Err(e) = dt2 {
        println!("{}", e);
    }

    let dt = NaiveDate::from_ymd(2015, 9, 5).and_hms_milli(23, 56, 4, 0);
    println!("{}", dt.format(fmt).to_string());
    let s = dt.format(fmt).to_string();
    let dt2 = NaiveDateTime::parse_from_str(&s, fmt);
    println!("{:?}", dt2);
    if let Err(e) = dt2 {
        println!("{}", e);
    }
}

It prints (and the first error is unexpected):

1441497364.649
Err(ParseError(Impossible))
no possible date and time matching input
1441497364.000
Ok(2015-09-05T23:56:04)
@quodlibetor
Copy link
Contributor

This is surprising, but I am able to reproduce and I would expect your code to work. I have limited internet/computer time for the next few days, but I will take a look asap.

If this is blocking you, a temporary workaround would be splitting on the dot, using %s and then adding a duration equal to the subsec part :-(

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