@@ -312,20 +312,36 @@ impl FileTimeMatcher {
312312 e. duration ( )
313313 }
314314 } ;
315+
316+ if self . today_start {
317+ // Relative to today's midnight, GNU's n-day interval is
318+ // [(n - 1) days, n days). Keep fractions at the boundaries and use
319+ // i128 so even u64::MAX days cannot overflow.
320+ let age_in_nanos = age. as_nanos ( ) as i128 * if is_negative { -1 } else { 1 } ;
321+ let nanos_per_day = i128:: from ( SECONDS_PER_DAY ) * 1_000_000_000 ;
322+ let boundary = |days| i128:: from ( days) * nanos_per_day;
323+ return Ok ( match self . days {
324+ ComparableValue :: EqualTo ( days) => {
325+ let end = boundary ( days) ;
326+ ( end - nanos_per_day..end) . contains ( & age_in_nanos)
327+ }
328+ ComparableValue :: MoreThan ( days) => age_in_nanos > boundary ( days) ,
329+ // GNU's -n also accepts the fractional second just before
330+ // the midnight boundary, but not the preceding whole second.
331+ ComparableValue :: LessThan ( days) => {
332+ age_in_nanos < boundary ( days) - nanos_per_day + 1_000_000_000
333+ }
334+ } ) ;
335+ }
336+
315337 let age_in_seconds: i64 = age. as_secs ( ) as i64 * if is_negative { -1 } else { 1 } ;
316338
317339 // rust division truncates towards zero (see
318340 // https://github.com/rust-lang/rust/blob/master/src/libcore/ops.rs#L580 )
319341 // so a simple age_in_seconds / SECONDS_PER_DAY gives the wrong answer
320342 // for negative ages: a file whose age is 1 second in the future needs to
321343 // count as -1 day old, not 0.
322- // If today_start is true, we should count it as 0 days old.
323- // because today is 00:00:00, so we need to subtract 1 day.
324- let negative_offset = if is_negative && !self . today_start {
325- -1
326- } else {
327- 0
328- } ;
344+ let negative_offset = if is_negative { -1 } else { 0 } ;
329345
330346 let age_in_days = age_in_seconds / SECONDS_PER_DAY + negative_offset;
331347 Ok ( self . days . imatches ( age_in_days) )
@@ -627,6 +643,61 @@ mod tests {
627643 assert_eq ! ( midnight. time( ) , NaiveTime :: from_hms_opt( 0 , 0 , 0 ) . unwrap( ) ) ;
628644 }
629645
646+ #[ rstest:: rstest]
647+ #[ case:: today( ComparableValue :: EqualTo ( 0 ) , 43_200 , 0 , true ) ]
648+ #[ case:: yesterday_is_not_today( ComparableValue :: EqualTo ( 0 ) , -43_200 , 0 , false ) ]
649+ #[ case:: yesterday( ComparableValue :: EqualTo ( 1 ) , -43_200 , 0 , true ) ]
650+ #[ case:: two_days_ago_is_not_yesterday( ComparableValue :: EqualTo ( 1 ) , -129_600 , 0 , false ) ]
651+ #[ case:: two_days_ago( ComparableValue :: EqualTo ( 2 ) , -129_600 , 0 , true ) ]
652+ #[ case:: midnight_is_not_today( ComparableValue :: EqualTo ( 0 ) , 0 , 0 , false ) ]
653+ #[ case:: after_midnight( ComparableValue :: EqualTo ( 0 ) , 0 , 500_000_000 , true ) ]
654+ #[ case:: next_midnight( ComparableValue :: EqualTo ( 0 ) , 86_400 , 0 , true ) ]
655+ #[ case:: after_next_midnight( ComparableValue :: EqualTo ( 0 ) , 86_400 , 500_000_000 , false ) ]
656+ #[ case:: previous_midnight( ComparableValue :: EqualTo ( 1 ) , -86_400 , 0 , false ) ]
657+ #[ case:: after_previous_midnight( ComparableValue :: EqualTo ( 1 ) , -86_400 , 500_000_000 , true ) ]
658+ #[ case:: midnight_is_yesterday( ComparableValue :: EqualTo ( 1 ) , 0 , 0 , true ) ]
659+ #[ case:: after_midnight_is_not_yesterday( ComparableValue :: EqualTo ( 1 ) , 0 , 500_000_000 , false ) ]
660+ #[ case:: older_than_today( ComparableValue :: MoreThan ( 0 ) , -43_200 , 0 , true ) ]
661+ #[ case:: midnight_is_not_older( ComparableValue :: MoreThan ( 0 ) , 0 , 0 , false ) ]
662+ #[ case:: before_midnight_is_older( ComparableValue :: MoreThan ( 0 ) , -1 , 500_000_000 , true ) ]
663+ #[ case:: previous_midnight_is_not_older( ComparableValue :: MoreThan ( 1 ) , -86_400 , 0 , false ) ]
664+ #[ case:: before_previous_midnight_is_older( ComparableValue :: MoreThan ( 1 ) , -86_401 , 500_000_000 , true ) ]
665+ #[ case:: less_than_one_day( ComparableValue :: LessThan ( 1 ) , 43_200 , 0 , true ) ]
666+ #[ case:: yesterday_is_not_less_than_one_day( ComparableValue :: LessThan ( 1 ) , -43_200 , 0 , false ) ]
667+ #[ case:: last_fractional_second( ComparableValue :: LessThan ( 1 ) , -1 , 500_000_000 , true ) ]
668+ #[ case:: one_second_before_midnight( ComparableValue :: LessThan ( 1 ) , -1 , 0 , false ) ]
669+ #[ case:: previous_last_fractional_second( ComparableValue :: LessThan ( 2 ) , -86_401 , 500_000_000 , true ) ]
670+ #[ case:: before_previous_last_second( ComparableValue :: LessThan ( 2 ) , -86_401 , 0 , false ) ]
671+ #[ case:: huge_equal( ComparableValue :: EqualTo ( u64 :: MAX ) , 0 , 0 , false ) ]
672+ #[ case:: huge_more( ComparableValue :: MoreThan ( u64 :: MAX ) , 0 , 0 , false ) ]
673+ #[ case:: huge_less( ComparableValue :: LessThan ( u64 :: MAX ) , 0 , 0 , true ) ]
674+ fn daystart_file_time_ranges (
675+ #[ case] days : ComparableValue ,
676+ #[ case] offset_seconds : i64 ,
677+ #[ case] nanos : u32 ,
678+ #[ case] expected : bool ,
679+ #[ values( FileTimeType :: Accessed , FileTimeType :: Modified ) ] file_time_type : FileTimeType ,
680+ ) {
681+ const MIDNIGHT : i64 = 1_700_006_400 ;
682+ let dir = tempfile:: tempdir ( ) . expect ( "create fixture directory" ) ;
683+ let path = dir. path ( ) . join ( "file" ) ;
684+ File :: create ( & path) . expect ( "create fixture file" ) ;
685+ let stamp = filetime:: FileTime :: from_unix_time ( MIDNIGHT + offset_seconds, nanos) ;
686+ filetime:: set_file_times ( & path, stamp, stamp) . expect ( "set fixture timestamps" ) ;
687+ let entry = get_dir_entry_for ( & dir. path ( ) . to_string_lossy ( ) , "file" ) ;
688+ let midnight = UNIX_EPOCH + Duration :: from_secs ( MIDNIGHT as u64 ) ;
689+
690+ // Expectations include GNU's strict +n boundary and the fractional
691+ // second accepted by -n just before midnight.
692+ let matcher = FileTimeMatcher :: new ( file_time_type, days, true ) ;
693+ assert_eq ! (
694+ matcher
695+ . matches_impl( & entry, midnight)
696+ . expect( "match file time" ) ,
697+ expected
698+ ) ;
699+ }
700+
630701 #[ test]
631702 fn file_time_matcher_modified_changed_accessed ( ) {
632703 let temp_dir = Builder :: new ( )
0 commit comments