Skip to content

Commit dab6003

Browse files
Merge pull request #6160 from mvo5/date-from-stdin
date: support `-f -` to read from stdin
2 parents 0ef06bd + af0ba86 commit dab6003

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/uu/date/src/date.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ enum DateSource {
9191
Now,
9292
Custom(String),
9393
File(PathBuf),
94+
Stdin,
9495
Human(TimeDelta),
9596
}
9697

@@ -173,7 +174,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
173174
DateSource::Custom(date.into())
174175
}
175176
} else if let Some(file) = matches.get_one::<String>(OPT_FILE) {
176-
DateSource::File(file.into())
177+
match file.as_ref() {
178+
"-" => DateSource::Stdin,
179+
_ => DateSource::File(file.into()),
180+
}
177181
} else {
178182
DateSource::Now
179183
};
@@ -240,6 +244,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
240244
}
241245
}
242246
}
247+
DateSource::Stdin => {
248+
let lines = BufReader::new(std::io::stdin()).lines();
249+
let iter = lines.map_while(Result::ok).map(parse_date);
250+
Box::new(iter)
251+
}
243252
DateSource::File(ref path) => {
244253
if path.is_dir() {
245254
return Err(USimpleError::new(

tests/by-util/test_date.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,21 @@ fn test_date_parse_from_format() {
426426
.arg("+%Y-%m-%d %H:%M:%S")
427427
.succeeds();
428428
}
429+
430+
#[test]
431+
fn test_date_from_stdin() {
432+
new_ucmd!()
433+
.arg("-f")
434+
.arg("-")
435+
.pipe_in(
436+
"2023-03-27 08:30:00\n\
437+
2023-04-01 12:00:00\n\
438+
2023-04-15 18:30:00\n",
439+
)
440+
.succeeds()
441+
.stdout_is(
442+
"Mon Mar 27 08:30:00 2023\n\
443+
Sat Apr 1 12:00:00 2023\n\
444+
Sat Apr 15 18:30:00 2023\n",
445+
);
446+
}

0 commit comments

Comments
 (0)