date: fix date -f dates.txt is failing (#6148)

* date: fix `date -f dates.txt is failing`

This commit is a trivial followup for:
https://github.com/uutils/coreutils/pull/4917
and
https://github.com/uutils/parse_datetime/pull/12

The functionality to parse the datetime was moved into the parse_datetime
crate and the only (tiny) piece left is to call it from `date`.

It also adds the test-case from the original issue. I did not include
the two tests from PR#4917 because they appear to work even without
this change. I am happy to include them of course if prefered.

Closes: #4657

Thanks to Ben Schofield

* tests: tweak changes to test_date.rs to be more idiomatic

Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>

---------

Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
This commit is contained in:
Michael Vogt 2024-03-30 15:17:10 +01:00 committed by GitHub
parent eca8bafcc1
commit 3a6bf34284
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 3 deletions

View file

@ -404,9 +404,8 @@ fn make_format_string(settings: &Settings) -> &str {
/// If it fails, return a tuple of the `String` along with its `ParseError`.
fn parse_date<S: AsRef<str> + Clone>(
s: S,
) -> Result<DateTime<FixedOffset>, (String, chrono::format::ParseError)> {
// TODO: The GNU date command can parse a wide variety of inputs.
s.as_ref().parse().map_err(|e| (s.as_ref().into(), e))
) -> Result<DateTime<FixedOffset>, (String, parse_datetime::ParseDateTimeError)> {
parse_datetime::parse_datetime(s.as_ref()).map_err(|e| (s.as_ref().into(), e))
}
#[cfg(not(any(unix, windows)))]

View file

@ -409,3 +409,20 @@ fn test_date_overflow() {
.no_stdout()
.stderr_contains("invalid date");
}
#[test]
fn test_date_parse_from_format() {
let (at, mut ucmd) = at_and_ucmd!();
const FILE: &str = "file-with-dates";
at.write(
FILE,
"2023-03-27 08:30:00\n\
2023-04-01 12:00:00\n\
2023-04-15 18:30:00",
);
ucmd.arg("-f")
.arg(at.plus(FILE))
.arg("+%Y-%m-%d %H:%M:%S")
.succeeds();
}