1
- use chrono:: { DateTime , Duration , Utc } ;
1
+ // cSpell:disable
2
+ use chrono:: { DateTime , Datelike , Duration , NaiveTime , Utc } ;
3
+ // cSpell:enable
2
4
// This file is part of the uutils coreutils package.
3
5
//
4
6
// For the full copyright and license information, please view the LICENSE
@@ -397,10 +399,13 @@ fn test_date_string_human() {
397
399
"30 minutes ago" ,
398
400
"10 seconds" ,
399
401
"last day" ,
402
+ "last monday" ,
400
403
"last week" ,
401
404
"last month" ,
402
405
"last year" ,
406
+ "this monday" ,
403
407
"next day" ,
408
+ "next monday" ,
404
409
"next week" ,
405
410
"next month" ,
406
411
"next year" ,
@@ -440,6 +445,37 @@ fn test_negative_offset() {
440
445
}
441
446
}
442
447
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
+
443
479
#[ test]
444
480
fn test_invalid_date_string ( ) {
445
481
new_ucmd ! ( )
@@ -448,6 +484,15 @@ fn test_invalid_date_string() {
448
484
. fails ( )
449
485
. no_stdout ( )
450
486
. 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" ) ;
451
496
}
452
497
453
498
#[ test]
0 commit comments