Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/uu/date/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,5 @@ date-error-format-modifier-width-too-large = format modifier width '{$width}' is
date-error-format-missing-plus = the argument {$arg} lacks a leading '+';
when using an option to specify date(s), any non-option
argument must be a format string beginning with '+'
date-error-print-and-set = the options to print and set the time may not be used together
12 changes: 11 additions & 1 deletion src/uu/date/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::sync::OnceLock;
use thiserror::Error;
use uucore::display::Quotable;
use uucore::error::FromIo;
use uucore::error::{UError, UResult, strip_errno};
use uucore::error::{UError, UResult, UUsageError, strip_errno};
#[cfg(feature = "i18n-datetime")]
use uucore::i18n::datetime::{localize_format_string, should_use_icu_locale};
use uucore::translate;
Expand Down Expand Up @@ -351,6 +351,16 @@ fn parse_military_timezone_with_offset(s: &str) -> Option<(i32, DayDelta)> {
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;

if matches.contains_id(OPT_SET)
&& [OPT_DATE, OPT_FILE, OPT_REFERENCE, OPT_RESOLUTION]
.iter()
.any(|option| {
matches.value_source(option) == Some(clap::parser::ValueSource::CommandLine)
})
{
return Err(UUsageError::new(1, translate!("date-error-print-and-set")));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it able to do same things by clap's confliction? Patching error message of GnuTetst is allowed if needed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Replaced the manual check and custom message with clap conflicts in 6e1afe3. The full date suite passes: 164 passed, 7 ignored; Clippy also passes.

let date_source = if let Some(date_os) = matches.get_one::<OsString>(OPT_DATE) {
// Convert OsString to String, handling invalid UTF-8 with GNU-compatible error
let date = date_os.to_str().ok_or_else(|| DateError::InvalidDate {
Expand Down
25 changes: 25 additions & 0 deletions tests/by-util/test_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3446,3 +3446,28 @@ fn test_non_utf8_operands_are_octal_escaped() {
.stderr_contains(expected);
}
}

#[test]
fn test_date_print_and_set_conflict() {
// Invalid set input also prevents any clock change if validation regresses.
for print_args in [
vec!["-d", "@0"],
vec!["-f", "missing"],
vec!["-r", "missing"],
vec!["--resolution"],
] {
for set_first in [true, false] {
let mut args = vec!["-s", "not-a-date"];
if set_first {
args.extend(&print_args);
} else {
args.splice(0..0, print_args.iter().copied());
}
new_ucmd!()
.args(&args)
.fails_with_code(1)
.no_stdout()
.stderr_contains("the options to print and set the time may not be used together");
}
}
}
Loading