From 57d9dd7c6fb1f0912ddc9c2168a64a3a7b357cb1 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Sat, 30 Mar 2024 10:24:35 +0100 Subject: [PATCH 1/2] 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 --- src/uu/date/src/date.rs | 5 ++--- tests/by-util/test_date.rs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/uu/date/src/date.rs b/src/uu/date/src/date.rs index bc50a8a2c58..76b245923dd 100644 --- a/src/uu/date/src/date.rs +++ b/src/uu/date/src/date.rs @@ -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 + Clone>( s: S, -) -> Result, (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, (String, parse_datetime::ParseDateTimeError)> { + parse_datetime::parse_datetime(s.as_ref()).map_err(|e| (s.as_ref().into(), e)) } #[cfg(not(any(unix, windows)))] diff --git a/tests/by-util/test_date.rs b/tests/by-util/test_date.rs index def3fa8af00..b01d8088150 100644 --- a/tests/by-util/test_date.rs +++ b/tests/by-util/test_date.rs @@ -409,3 +409,21 @@ 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"; + + std::fs::write( + at.plus(FILE), + "2023-03-27 08:30:00\n\ + 2023-04-01 12:00:00\n\ + 2023-04-15 18:30:00", + ) + .unwrap(); + ucmd.arg("-f") + .arg(at.plus(FILE)) + .arg("+%Y-%m-%d %H:%M:%S") + .succeeds(); +} From 4b5022cfe98cb8e9a25e7e5b23d0e049e8464f57 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Sat, 30 Mar 2024 11:55:09 +0100 Subject: [PATCH 2/2] tests: tweak changes to test_date.rs to be more idiomatic Co-authored-by: Sylvestre Ledru --- tests/by-util/test_date.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/by-util/test_date.rs b/tests/by-util/test_date.rs index b01d8088150..f45b81cb585 100644 --- a/tests/by-util/test_date.rs +++ b/tests/by-util/test_date.rs @@ -415,13 +415,12 @@ fn test_date_parse_from_format() { let (at, mut ucmd) = at_and_ucmd!(); const FILE: &str = "file-with-dates"; - std::fs::write( - at.plus(FILE), + at.write( + FILE, "2023-03-27 08:30:00\n\ 2023-04-01 12:00:00\n\ 2023-04-15 18:30:00", - ) - .unwrap(); + ); ucmd.arg("-f") .arg(at.plus(FILE)) .arg("+%Y-%m-%d %H:%M:%S")