Skip to content

Commit 3befb16

Browse files
BenWiederhakesylvestre
authored andcommitted
date: improve range of accepted values for --set
1 parent e93b7a5 commit 3befb16

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/uu/date/src/date.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ struct Settings {
7474
utc: bool,
7575
format: Format,
7676
date_source: DateSource,
77-
set_to: Option<DateTime<FixedOffset>>,
77+
set_to: Option<String>,
7878
}
7979

8080
/// Various ways of displaying the date
@@ -185,22 +185,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
185185
DateSource::Now
186186
};
187187

188-
let set_to = match matches.get_one::<String>(OPT_SET).map(parse_date) {
189-
None => None,
190-
Some(Err((input, _err))) => {
191-
return Err(USimpleError::new(
192-
1,
193-
format!("invalid date {}", input.quote()),
194-
));
195-
}
196-
Some(Ok(date)) => Some(date),
197-
};
188+
let set_to = matches.get_one::<String>(OPT_SET);
198189

199190
let settings = Settings {
200191
utc: matches.get_flag(OPT_UNIVERSAL),
201192
format,
202193
date_source,
203-
set_to,
194+
set_to: set_to.cloned(),
204195
};
205196

206197
// Get the current time, either in the local time zone or UTC.
@@ -213,7 +204,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
213204
};
214205

215206
// Iterate over all dates - whether it's a single date or a file.
216-
let dates: Box<dyn Iterator<Item = _>> = if let Some(date) = settings.set_to {
207+
let dates: Box<dyn Iterator<Item = _>> = if let Some(date_string) = &settings.set_to {
208+
let date = parse_datetime::parse_datetime_at_date(now.into(), date_string)
209+
.map_err(|_| USimpleError::new(1, format!("invalid date {}", date_string.quote())))?;
217210
// All set time functions expect UTC datetimes.
218211
let date: DateTime<Utc> = if settings.utc {
219212
date.with_timezone(&Utc)

tests/by-util/test_date.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,27 @@ fn test_date_set_permissions_error() {
284284
}
285285
}
286286

287+
#[test]
288+
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
289+
fn test_date_set_permissions_error_interpreted() {
290+
// This implicitly tests that the given strings are interpreted as valid dates,
291+
// because parsing errors would have been discovered earlier in the process.
292+
if !(geteuid() == 0 || uucore::os::is_wsl_1()) {
293+
for date_string in [
294+
"yesterday",
295+
// TODO "a fortnight ago",
296+
"42 days",
297+
"2001-02-03",
298+
"20010203",
299+
// TODO "02/03/2001",
300+
] {
301+
let result = new_ucmd!().arg("-s").arg(date_string).fails();
302+
// stdout depends on the specific date; don't check it.
303+
assert!(result.stderr_str().starts_with("date: cannot set date: "));
304+
}
305+
}
306+
}
307+
287308
#[test]
288309
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
289310
fn test_date_set_permissions_error_repeated() {

0 commit comments

Comments
 (0)