From 526079b5d330ea46757c198e6aaf6813ce41e2b4 Mon Sep 17 00:00:00 2001 From: Phillips Horselover <63288135+NaviHX@users.noreply.github.com> Date: Sat, 19 Oct 2024 14:33:37 +0800 Subject: [PATCH 1/2] find/time: calculate midnight with local time zone (#467) * find/time: calculate midnight with local time zone * find/time: remove deprecated functions * find/time: add test for local midnight calculation --- src/find/matchers/time.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/find/matchers/time.rs b/src/find/matchers/time.rs index 260219bc..e7d07fbc 100644 --- a/src/find/matchers/time.rs +++ b/src/find/matchers/time.rs @@ -9,6 +9,8 @@ use std::fs::{self, Metadata}; use std::io::{stderr, Write}; use std::time::{Duration, SystemTime, UNIX_EPOCH}; +use chrono::{DateTime, Local, Timelike}; + #[cfg(unix)] use std::os::unix::fs::MetadataExt; @@ -19,10 +21,14 @@ const SECONDS_PER_DAY: i64 = 60 * 60 * 24; 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 seconds_since_last_midnight = local_time.num_seconds_from_midnight(); + let local_midnight_seconds = local_time.timestamp() - seconds_since_last_midnight as i64; + + UNIX_EPOCH + Duration::from_secs(local_midnight_seconds as u64) } else { matcher_io.now() } @@ -396,6 +402,7 @@ impl FileAgeRangeMatcher { #[cfg(test)] mod tests { + use chrono::NaiveTime; use std::fs; use std::fs::{File, OpenOptions}; use std::io::Read; @@ -616,6 +623,15 @@ mod tests { ); } + #[test] + fn get_local_midnight() { + let deps = FakeDependencies::new(); + let midnight = get_time(&mut deps.new_matcher_io(), true); + + let midnight = DateTime::::from(midnight); + assert_eq!(midnight.time(), NaiveTime::from_hms_opt(0, 0, 0).unwrap()) + } + #[test] fn file_time_matcher_modified_changed_accessed() { let temp_dir = Builder::new() From 2e9e90887f94cfedae9e7c726af3e89998a0ce65 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 19 Oct 2024 14:53:16 +0800 Subject: [PATCH 2/2] build(deps): bump tempfile from 3.12.0 to 3.13.0 (#458) Bumps [tempfile](https://github.com/Stebalien/tempfile) from 3.12.0 to 3.13.0. - [Changelog](https://github.com/Stebalien/tempfile/blob/master/CHANGELOG.md) - [Commits](https://github.com/Stebalien/tempfile/compare/v3.12.0...v3.13.0) --- updated-dependencies: - dependency-name: tempfile dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: hanbings --- Cargo.lock | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 44d2ffe3..11bfb0eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -250,9 +250,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.0.1" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" +checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" [[package]] name = "filetime" @@ -404,9 +404,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.155" +version = "0.2.159" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" [[package]] name = "libredox" @@ -421,9 +421,9 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.4.12" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" [[package]] name = "lock_api" @@ -680,9 +680,9 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "rustix" -version = "0.38.31" +version = "0.38.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" +checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" dependencies = [ "bitflags 2.4.1", "errno", @@ -783,9 +783,9 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.12.0" +version = "3.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04cbcdd0c794ebb0d4cf35e88edd2f7d2c4c3e9a5a6dab322839b321c6a87a64" +checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b" dependencies = [ "cfg-if", "fastrand",