uutils / findutils

Rust implementation of findutils
MIT License
314 stars 38 forks source link

`-daystart` option does not calculate midnight with local time zone #466

Closed NaviHX closed 1 month ago

NaviHX commented 1 month ago

When using the -daystart option with the find command, today's midnight is incorrect for users whose local time zone differs from UTC. Specifically, instead of considering the local time zone, the implementation calculates midnight by using the following code:

let midnight_seconds = seconds - (seconds % 86400);
// eprintln!("Midnight seconds: {}", midnight_seconds);

This code causes the calculated midnight_seconds is always based on UNIX_EPOCH (00:00 +0000). However, UNIX_EPOCH is not midnight in other time zones. For example, run find -mtime 0 at Wed, 16 Oct 2024 14:27:57 +0800 and the above code will output:

Midnight seconds: 1729036800
# > date -d "@1729036800" -R
# Wed, 16 Oct 2024 08:00:00 +0800
NaviHX commented 1 month ago

I have a workaround here:

https://github.com/uutils/findutils/compare/dce8b2dd...fcb8da254

 fn get_time(matcher_io: &mut MatcherIO, today_start: bool) -> SystemTime {
     if today_start {
         // the time at 00:00:00 of today
-        let duration = matcher_io.now().duration_since(UNIX_EPOCH).unwrap();
-        let seconds = duration.as_secs();
-        let midnight_seconds = seconds - (seconds % 86400);
-        UNIX_EPOCH + Duration::from_secs(midnight_seconds)
+        let duration_since_unix_epoch = matcher_io.now().duration_since(UNIX_EPOCH).unwrap();
+        let seconds_since_unix_epoch = duration_since_unix_epoch.as_secs();
+        let utc_time = DateTime::from_timestamp(seconds_since_unix_epoch as i64, 0).unwrap();
+        let local_time = utc_time.with_timezone(&Local);
+        let local_midnight = local_time.date().and_hms(0, 0, 0);
+        let local_midnight_seconds = local_midnight.timestamp();
+
+        UNIX_EPOCH + Duration::from_secs(local_midnight_seconds as u64)
     } else {
         matcher_io.now()
     }
sylvestre commented 1 month ago

please submit a PR :)