Skip to content

date: allow negative date offsets #7548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/uu/date/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ pub fn uu_app() -> Command {
.short('d')
.long(OPT_DATE)
.value_name("STRING")
.allow_hyphen_values(true)
.help("display time described by STRING, not 'now'"),
)
.arg(
Expand Down
26 changes: 26 additions & 0 deletions tests/by-util/test_date.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::{DateTime, Duration, Utc};
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
Expand Down Expand Up @@ -410,6 +411,31 @@ fn test_date_string_human() {
}
}

#[test]
fn test_negative_offset() {
let data_formats = vec![
("-1 hour", Duration::hours(1)),
("-1 hours", Duration::hours(1)),
("-1 day", Duration::days(1)),
("-2 weeks", Duration::weeks(2)),
];
for (date_format, offset) in data_formats {
new_ucmd!()
.arg("-d")
.arg(date_format)
.arg("--rfc-3339=seconds")
.succeeds()
.stdout_str_check(|out| {
let date = DateTime::parse_from_rfc3339(out.trim()).unwrap();

// Is the resulting date roughly what is expected?
let expected_date = Utc::now() - offset;
date > expected_date - Duration::minutes(10)
&& date < expected_date + Duration::minutes(10)
});
}
}

#[test]
fn test_invalid_date_string() {
new_ucmd!()
Expand Down
Loading