1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

date: fix format literal for nanoseconds

This commit is contained in:
Jan Scheer 2021-05-12 10:21:24 +02:00
parent 8200d399e8
commit 12a43d6eb3
2 changed files with 10 additions and 3 deletions

View file

@ -215,8 +215,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
eprintln!("date: invalid date {}", form);
return 1;
}
// GNU `date` uses `%N` for nano seconds, however crate::chrono uses `%f`
let form = form[1..].replace("%N", "%f");
let form = form[1..].to_string();
Format::Custom(form)
} else if let Some(fmt) = matches
.values_of(OPT_ISO_8601)
@ -302,7 +301,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
for date in dates {
match date {
Ok(date) => {
let formatted = date.format(format_string);
// GNU `date` uses `%N` for nano seconds, however crate::chrono uses `%f`
let format_string = &format_string.replace("%N", "%f");
let formatted = date.format(format_string).to_string().replace("%f", "%N");
println!("{}", formatted);
}
Err((input, _err)) => {

View file

@ -121,6 +121,12 @@ fn test_date_format_without_plus() {
.code_is(1);
}
#[test]
fn test_date_format_literal() {
new_ucmd!().arg("+%%s").succeeds().stdout_is("%s\n");
new_ucmd!().arg("+%%N").succeeds().stdout_is("%N\n");
}
#[test]
#[cfg(all(unix, not(target_os = "macos")))]
fn test_date_set_valid() {