Skip to content

Commit

Permalink
Encapsulates the time acquisition function used to process -daystart.
Browse files Browse the repository at this point in the history
  • Loading branch information
hanbings committed Jul 6, 2024
1 parent a95bf69 commit d5af780
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions src/find/matchers/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ impl Matcher for NewerTimeMatcher {
}
}

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)
} else {
matcher_io.now()
}
}

#[derive(Clone, Copy, Debug)]
pub enum FileTimeType {
Accessed,
Expand Down Expand Up @@ -231,16 +243,8 @@ pub struct FileTimeMatcher {

impl Matcher for FileTimeMatcher {
fn matches(&self, file_info: &DirEntry, matcher_io: &mut MatcherIO) -> bool {
let time = if self.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)
} else {
matcher_io.now()
};
match self.matches_impl(file_info, time) {
let start_time = get_time(matcher_io, self.today_start);
match self.matches_impl(file_info, start_time) {
Err(e) => {
writeln!(
&mut stderr(),
Expand Down Expand Up @@ -312,16 +316,8 @@ pub struct FileAgeRangeMatcher {

impl Matcher for FileAgeRangeMatcher {
fn matches(&self, file_info: &DirEntry, matcher_io: &mut MatcherIO) -> bool {
let time = if self.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)
} else {
matcher_io.now()
};
match self.matches_impl(file_info, time) {
let start_time = get_time(matcher_io, self.today_start);
match self.matches_impl(file_info, start_time) {
Err(e) => {
writeln!(
&mut stderr(),
Expand Down

0 comments on commit d5af780

Please sign in to comment.