Merge pull request #2205 from jhscheer/issue_2204

date: fix format for nanoseconds
This commit is contained in:
Sylvestre Ledru 2021-05-11 23:44:29 +02:00 committed by GitHub
commit e16df57206
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 4 deletions

View file

@ -207,11 +207,16 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.alias(OPT_UNIVERSAL_2)
.help("print or set Coordinated Universal Time (UTC)"),
)
.arg(Arg::with_name(OPT_FORMAT).multiple(true))
.arg(Arg::with_name(OPT_FORMAT).multiple(false))
.get_matches_from(args);
let format = if let Some(form) = matches.value_of(OPT_FORMAT) {
let form = form[1..].into();
if !form.starts_with('+') {
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");
Format::Custom(form)
} else if let Some(fmt) = matches
.values_of(OPT_ISO_8601)
@ -237,7 +242,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let set_to = match matches.value_of(OPT_SET).map(parse_date) {
None => None,
Some(Err((input, _err))) => {
eprintln!("date: invalid date '{}'", input);
eprintln!("date: invalid date {}", input);
return 1;
}
Some(Ok(date)) => Some(date),
@ -301,7 +306,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
println!("{}", formatted);
}
Err((input, _err)) => {
println!("date: invalid date '{}'", input);
println!("date: invalid date {}", input);
}
}
}

View file

@ -104,6 +104,23 @@ fn test_date_format_full_day() {
.stdout_matches(&re);
}
#[test]
fn test_date_nano_seconds() {
// %N nanoseconds (000000000..999999999)
let re = Regex::new(r"^\d{1,9}$").unwrap();
new_ucmd!().arg("+%N").succeeds().stdout_matches(&re);
}
#[test]
fn test_date_format_without_plus() {
// [+FORMAT]
new_ucmd!()
.arg("%s")
.fails()
.stderr_contains("date: invalid date %s")
.code_is(1);
}
#[test]
#[cfg(all(unix, not(target_os = "macos")))]
fn test_date_set_valid() {