Skip to content

Commit

Permalink
Merge pull request #6160 from mvo5/date-from-stdin
Browse files Browse the repository at this point in the history
date: support `-f -` to read from stdin
  • Loading branch information
BenWiederhake authored Mar 31, 2024
2 parents 0ef06bd + af0ba86 commit dab6003
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/uu/date/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ enum DateSource {
Now,
Custom(String),
File(PathBuf),
Stdin,
Human(TimeDelta),
}

Expand Down Expand Up @@ -173,7 +174,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
DateSource::Custom(date.into())
}
} else if let Some(file) = matches.get_one::<String>(OPT_FILE) {
DateSource::File(file.into())
match file.as_ref() {
"-" => DateSource::Stdin,
_ => DateSource::File(file.into()),
}
} else {
DateSource::Now
};
Expand Down Expand Up @@ -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(
Expand Down
18 changes: 18 additions & 0 deletions tests/by-util/test_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
}

0 comments on commit dab6003

Please sign in to comment.