1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #6160 from mvo5/date-from-stdin

date: support `-f -` to read from stdin
This commit is contained in:
Ben Wiederhake 2024-03-31 22:36:19 +02:00 committed by GitHub
commit dab6003b52
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 1 deletions

View file

@ -91,6 +91,7 @@ enum DateSource {
Now, Now,
Custom(String), Custom(String),
File(PathBuf), File(PathBuf),
Stdin,
Human(TimeDelta), Human(TimeDelta),
} }
@ -173,7 +174,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
DateSource::Custom(date.into()) DateSource::Custom(date.into())
} }
} else if let Some(file) = matches.get_one::<String>(OPT_FILE) { } 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 { } else {
DateSource::Now 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) => { DateSource::File(ref path) => {
if path.is_dir() { if path.is_dir() {
return Err(USimpleError::new( return Err(USimpleError::new(

View file

@ -426,3 +426,21 @@ fn test_date_parse_from_format() {
.arg("+%Y-%m-%d %H:%M:%S") .arg("+%Y-%m-%d %H:%M:%S")
.succeeds(); .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",
);
}