Skip to content

Commit d33d731

Browse files
authored
Merge pull request #7548 from lewisboon/bugfix/date-negative-offset
date: allow negative date offsets
2 parents 1898032 + b02e3d5 commit d33d731

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
@@ -412,6 +413,31 @@ fn test_date_string_human() {
412413
}
413414
}
414415

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

0 commit comments

Comments
 (0)