From af0ba8665793594f72f397318984723b9b6d5bf9 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Sun, 31 Mar 2024 21:15:46 +0200 Subject: [PATCH] date: support `-f -` to read from stdin So far `date -f -` was not reading from stdin. This commit fixes this. Closes: #6058 --- src/uu/date/src/date.rs | 11 ++++++++++- tests/by-util/test_date.rs | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/uu/date/src/date.rs b/src/uu/date/src/date.rs index 76b245923..5d6e8fd22 100644 --- a/src/uu/date/src/date.rs +++ b/src/uu/date/src/date.rs @@ -91,6 +91,7 @@ enum DateSource { Now, Custom(String), File(PathBuf), + Stdin, Human(TimeDelta), } @@ -173,7 +174,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { DateSource::Custom(date.into()) } } else if let Some(file) = matches.get_one::(OPT_FILE) { - DateSource::File(file.into()) + match file.as_ref() { + "-" => DateSource::Stdin, + _ => DateSource::File(file.into()), + } } else { DateSource::Now }; @@ -240,6 +244,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } } } + DateSource::Stdin => { + let lines = BufReader::new(std::io::stdin()).lines(); + let iter = lines.map_while(Result::ok).map(parse_date); + Box::new(iter) + } DateSource::File(ref path) => { if path.is_dir() { return Err(USimpleError::new( diff --git a/tests/by-util/test_date.rs b/tests/by-util/test_date.rs index f45b81cb5..16a01c655 100644 --- a/tests/by-util/test_date.rs +++ b/tests/by-util/test_date.rs @@ -426,3 +426,21 @@ fn test_date_parse_from_format() { .arg("+%Y-%m-%d %H:%M:%S") .succeeds(); } + +#[test] +fn test_date_from_stdin() { + new_ucmd!() + .arg("-f") + .arg("-") + .pipe_in( + "2023-03-27 08:30:00\n\ + 2023-04-01 12:00:00\n\ + 2023-04-15 18:30:00\n", + ) + .succeeds() + .stdout_is( + "Mon Mar 27 08:30:00 2023\n\ + Sat Apr 1 12:00:00 2023\n\ + Sat Apr 15 18:30:00 2023\n", + ); +}