coreutils
coreutils copied to clipboard
date: format date to correct date format before parsing
closes #6392
GNU testsuite comparison:
Skipping an intermittent issue tests/tail/inotify-dir-recreate (passes in this run but fails in the 'main' branch)
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.
You mean the bug is in parse datetime crate? @RenjiSann
Yes, it most probably is.
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)
Here is a PR for the fix in parse_datetime
https://github.com/uutils/parse_datetime/pull/76
Waiting for https://github.com/uutils/parse_datetime/pull/76 to be released
#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