date: fix inconsistent input parsing between -s and -d flags (#9690)

Add allow_hyphen_values(true) to -s flag to accept hyphen-prefixed values like '-3 days', making it consistent with -d flag behavior and GNU coreutils compatibility.

Fixes #9679
This commit is contained in:
nirv
2025-12-19 08:48:08 +01:00
committed by GitHub
parent 15d51c8804
commit 64478acdbf
2 changed files with 22 additions and 0 deletions
+1
View File
@@ -487,6 +487,7 @@ pub fn uu_app() -> Command {
.short('s')
.long(OPT_SET)
.value_name("STRING")
.allow_hyphen_values(true)
.help({
#[cfg(not(any(target_os = "macos", target_os = "redox")))]
{
+21
View File
@@ -293,6 +293,27 @@ fn test_date_set_permissions_error() {
}
}
#[test]
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
fn test_date_set_hyphen_prefixed_values() {
// test -s flag accepts hyphen-prefixed values like "-3 days"
if !(geteuid() == 0 || uucore::os::is_wsl_1()) {
let test_cases = vec!["-1 hour", "-2 days", "-3 weeks", "-1 month"];
for date_str in test_cases {
let result = new_ucmd!().arg("--set").arg(date_str).fails();
result.no_stdout();
// permission error, not argument parsing error
assert!(
result.stderr_str().starts_with("date: cannot set date: "),
"Expected permission error for '{}', but got: {}",
date_str,
result.stderr_str()
);
}
}
}
#[test]
#[cfg(target_os = "macos")]
fn test_date_set_mac_unavailable() {