Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/cargo/clap-4.5.20
Browse files Browse the repository at this point in the history
  • Loading branch information
hanbings authored Oct 19, 2024
2 parents f8b32e7 + 11a0b64 commit 173e651
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions src/find/matchers/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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::<Local>::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()
Expand Down

0 comments on commit 173e651

Please sign in to comment.