diff --git a/src/uu/date/src/date.rs b/src/uu/date/src/date.rs index d2100fc80..45bceaec3 100644 --- a/src/uu/date/src/date.rs +++ b/src/uu/date/src/date.rs @@ -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")))] { diff --git a/tests/by-util/test_date.rs b/tests/by-util/test_date.rs index 9d59efd58..512c5c799 100644 --- a/tests/by-util/test_date.rs +++ b/tests/by-util/test_date.rs @@ -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() {