coreutils icon indicating copy to clipboard operation
coreutils copied to clipboard

date: format date to correct date format before parsing

Open ahmadabd opened this issue 9 months ago • 8 comments

closes #6392

ahmadabd avatar May 14 '24 16:05 ahmadabd

GNU testsuite comparison:

Skipping an intermittent issue tests/tail/inotify-dir-recreate (passes in this run but fails in the 'main' branch)

github-actions[bot] avatar May 14 '24 17:05 github-actions[bot]

After looking at the code, there is this snippet in the parse_datetime_at_date function that checks 1999-1-4 against the ISO 8601 pattern.

For some reasons (I may not have all the context about it), we add "0000" to the candidate and "%H%M" to the string, which makes us check 1999-1-40000 against %Y-%m-%d%H%M.

I could understand why parsing 40000 as %d%H%M could fail, so maybe the error comes from here.

Maybe a workaround would be to add respectively " 0000" and " %H%M" to the candidate and pattern, so the parsing does not eat a 0 as part of the day.

This would require a change to the parse_datetime crate.

RenjiSann avatar May 14 '24 20:05 RenjiSann

You mean the bug is in parse datetime crate? @RenjiSann

ahmadabd avatar May 18 '24 06:05 ahmadabd

Yes, it most probably is.

RenjiSann avatar May 18 '24 06:05 RenjiSann

The following example isolates the bug being on the parsing of the last single digit. It has no issue parsing -1- as a month.

use parse_datetime::parse_datetime;

fn main() {

    let one = parse_datetime("2001-01-04");
    let two = parse_datetime("2001-1-04");
    let three = parse_datetime("2001-01-4");
    let four = parse_datetime("2001-1-4");

    println!("Result:\n{:?}\n{:?}\n{:?}\n{:?}", one, two, three, four);
}
❯ cargo run
   Compiling test_parse_date_time v0.1.0 (/tmp/test_parse_date_time)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.47s
     Running `target/debug/test_parse_date_time`
Result:
Ok(2001-01-04T00:00:00+02:00)
Ok(2001-01-04T00:00:00+02:00)
Err(InvalidInput)
Err(InvalidInput)

RenjiSann avatar May 18 '24 08:05 RenjiSann

Here is a PR for the fix in parse_datetime https://github.com/uutils/parse_datetime/pull/76

RenjiSann avatar May 18 '24 09:05 RenjiSann

Waiting for https://github.com/uutils/parse_datetime/pull/76 to be released

RenjiSann avatar May 20 '24 15:05 RenjiSann

#6423 was merged, from what I am testing, this is fixed.

Maybe we can still add tests to make sure there are no regressions afterwards ?

@ahmadabd do you want to give it a shot ? You can take a look at the tests I added in parse_datetime

RenjiSann avatar May 22 '24 09:05 RenjiSann