Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find/time: calculate midnight with local time zone #467

Merged
merged 3 commits into from
Oct 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading