Skip to content

Commit

Permalink
fix(resources): broken week day calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
RouHim committed Sep 18, 2023
1 parent 0e4d164 commit 5b260ce
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/resource_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use std::path::Path;

use chrono::{Datelike, Local, NaiveDateTime};
use exif::Exif;
use now::DateTimeNow;

use rayon::iter::IntoParallelRefIterator;
use rayon::iter::ParallelIterator;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -57,16 +55,15 @@ impl ImageResource {
None => return false,
};

let now = Local::now();
let begin_of_week = now.beginning_of_week().date_naive();
let end_of_week = now.end_of_week().date_naive();

// Only compare day and month, not year
// Because we want to compare the same day range in the past years
let taken_date = taken_date.with_year(now.year()).unwrap();
let now = Local::now().naive_local().date();
let current_weekday = now.weekday();
let days_since_monday = current_weekday.num_days_from_monday();
let start_of_week = now - chrono::Duration::days(days_since_monday as i64);
let end_of_week = start_of_week + chrono::Duration::days(6);

// Check if the taken date is between the begin and end of the week, thus this week
taken_date >= begin_of_week && taken_date <= end_of_week
taken_date.month() == start_of_week.month()
&& taken_date.day() >= start_of_week.day()
&& taken_date.day() <= end_of_week.day()
}
}

Expand Down

0 comments on commit 5b260ce

Please sign in to comment.