Skip to content

Commit fe93dae

Browse files
committed
date: add tests for relative weekdays
1 parent 0ec36be commit fe93dae

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

tests/by-util/test_date.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use chrono::{DateTime, Duration, Utc};
1+
// cSpell:disable
2+
use chrono::{DateTime, Datelike, Duration, NaiveTime, Utc};
3+
// cSpell:enable
24
// This file is part of the uutils coreutils package.
35
//
46
// For the full copyright and license information, please view the LICENSE
@@ -397,10 +399,13 @@ fn test_date_string_human() {
397399
"30 minutes ago",
398400
"10 seconds",
399401
"last day",
402+
"last monday",
400403
"last week",
401404
"last month",
402405
"last year",
406+
"this monday",
403407
"next day",
408+
"next monday",
404409
"next week",
405410
"next month",
406411
"next year",
@@ -440,6 +445,37 @@ fn test_negative_offset() {
440445
}
441446
}
442447

448+
#[test]
449+
fn test_relative_weekdays() {
450+
// Truncate time component to midnight
451+
let today = Utc::now().with_time(NaiveTime::MIN).unwrap();
452+
// Loop through each day of the week, starting with today
453+
for offset in 0..7 {
454+
for direction in ["last", "this", "next"] {
455+
let weekday = (today + Duration::days(offset))
456+
.weekday()
457+
.to_string()
458+
.to_lowercase();
459+
new_ucmd!()
460+
.arg("-d")
461+
.arg(format!("{} {}", direction, weekday))
462+
.arg("--rfc-3339=seconds")
463+
.arg("--utc")
464+
.succeeds()
465+
.stdout_str_check(|out| {
466+
let result = DateTime::parse_from_rfc3339(out.trim()).unwrap().to_utc();
467+
let expected = match (direction, offset) {
468+
("last", _) => today - Duration::days(7 - offset),
469+
("this", 0) => today,
470+
("next", 0) => today + Duration::days(7),
471+
_ => today + Duration::days(offset),
472+
};
473+
result == expected
474+
});
475+
}
476+
}
477+
}
478+
443479
#[test]
444480
fn test_invalid_date_string() {
445481
new_ucmd!()
@@ -448,6 +484,15 @@ fn test_invalid_date_string() {
448484
.fails()
449485
.no_stdout()
450486
.stderr_contains("invalid date");
487+
488+
new_ucmd!()
489+
.arg("-d")
490+
// cSpell:disable
491+
.arg("this fooday")
492+
// cSpell:enable
493+
.fails()
494+
.no_stdout()
495+
.stderr_contains("invalid date");
451496
}
452497

453498
#[test]

0 commit comments

Comments
 (0)