Skip to content

Commit b02e3d5

Browse files
committed
date: allow negative date offsets
Issue #7515 Clap needs to be specifically configured to allow values with a leading hyphen.
1 parent 105042f commit b02e3d5

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/uu/date/src/date.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ pub fn uu_app() -> Command {
318318
.short('d')
319319
.long(OPT_DATE)
320320
.value_name("STRING")
321+
.allow_hyphen_values(true)
321322
.help("display time described by STRING, not 'now'"),
322323
)
323324
.arg(

tests/by-util/test_date.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use chrono::{DateTime, Duration, Utc};
12
// This file is part of the uutils coreutils package.
23
//
34
// For the full copyright and license information, please view the LICENSE
@@ -410,6 +411,31 @@ fn test_date_string_human() {
410411
}
411412
}
412413

414+
#[test]
415+
fn test_negative_offset() {
416+
let data_formats = vec![
417+
("-1 hour", Duration::hours(1)),
418+
("-1 hours", Duration::hours(1)),
419+
("-1 day", Duration::days(1)),
420+
("-2 weeks", Duration::weeks(2)),
421+
];
422+
for (date_format, offset) in data_formats {
423+
new_ucmd!()
424+
.arg("-d")
425+
.arg(date_format)
426+
.arg("--rfc-3339=seconds")
427+
.succeeds()
428+
.stdout_str_check(|out| {
429+
let date = DateTime::parse_from_rfc3339(out.trim()).unwrap();
430+
431+
// Is the resulting date roughly what is expected?
432+
let expected_date = Utc::now() - offset;
433+
date > expected_date - Duration::minutes(10)
434+
&& date < expected_date + Duration::minutes(10)
435+
});
436+
}
437+
}
438+
413439
#[test]
414440
fn test_invalid_date_string() {
415441
new_ucmd!()

0 commit comments

Comments
 (0)