Skip to content

Commit dc4f648

Browse files
committed
use utc instead of local time
1 parent 2946dab commit dc4f648

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/daily_task/mod.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
1-
use chrono::{Datelike, Local, NaiveDate, NaiveTime};
1+
use chrono::{Datelike, NaiveDate, NaiveTime};
22
use chrono_tz::Asia::Kolkata;
33
use sqlx::PgPool;
44
use std::sync::Arc;
55
use tokio::time::sleep_until;
6-
use tracing::{debug, error};
6+
use tracing::{debug, error, info};
77

88
use crate::models::member::Member;
99

1010
/// Continuously sleep till midnight, then run the 'execute_daily_task' function.
1111
pub async fn run_daily_task_at_midnight(pool: Arc<PgPool>) {
1212
loop {
13-
let now = chrono::Local::now().with_timezone(&Kolkata);
14-
let next_midnight = (now + chrono::Duration::days(1))
15-
.date_naive()
16-
.and_hms_opt(0, 30, 0)
17-
.unwrap();
18-
19-
let duration_until_midnight = next_midnight.signed_duration_since(now.naive_local());
13+
let now = chrono::Utc::now().with_timezone(&Kolkata);
14+
let naive_midnight =
15+
NaiveTime::from_hms_opt(00, 30, 00).expect("Hardcoded time must be valid");
16+
let today_midnight = now
17+
.with_time(naive_midnight)
18+
.single()
19+
.expect("Hardcoded time must be valid");
20+
21+
let next_midnight = if now >= today_midnight {
22+
today_midnight + chrono::Duration::days(1)
23+
} else {
24+
today_midnight
25+
};
26+
debug!("next_midnight: {}", next_midnight);
27+
28+
let duration_until_midnight = next_midnight.signed_duration_since(now);
29+
info!("Sleeping for {}", duration_until_midnight.num_seconds());
2030
let sleep_duration =
2131
tokio::time::Duration::from_secs(duration_until_midnight.num_seconds() as u64);
2232

@@ -45,8 +55,8 @@ async fn execute_daily_task(pool: Arc<PgPool>) {
4555
// We need to add a record for every member because otherwise [`Presense`](https://www.github.com/presense) will only add present members to the DB, and we will have to JOIN Members and Attendance records for the day to get the absent members. In exchange for increased storage use, we get simpler queries for Home which needs the data for every member for every day so far. But as of Jan 2025, there are less than 50 members in the club and thus storage really shouldn't be an issue.
4656
/// Inserts new attendance records everyday for [`presense`](https://www.github.com/amfoss/presense) to update them later in the day and updates the AttendanceSummary table to keep track of monthly streaks.
4757
async fn update_attendance(members: Vec<Member>, pool: &PgPool) {
48-
debug!("Updating attendance...");
49-
let today = Local::now().with_timezone(&Kolkata).date_naive();
58+
let today = chrono::Utc::now().with_timezone(&Kolkata).date_naive();
59+
debug!("Updating attendance on {}", today);
5060

5161
for member in members {
5262
// Insert blank rows for each member
@@ -86,8 +96,8 @@ async fn update_attendance(members: Vec<Member>, pool: &PgPool) {
8696
/// Checks if the member was present yesterday, and if so, increments the `days_attended` value. Otherwise, do nothing.
8797
async fn update_attendance_summary(member_id: i32, pool: &PgPool) {
8898
debug!("Updating summary for member #{}", member_id);
89-
let today = chrono::Local::now().with_timezone(&Kolkata).date_naive();
90-
let yesterday = today.checked_sub_signed(chrono::Duration::days(1)).unwrap(); // Get yesterday's date
99+
let today = chrono::Utc::now().with_timezone(&Kolkata).date_naive();
100+
let yesterday = today.pred_opt().expect("Time must be valid");
91101

92102
// Check if the member was present yesterday
93103
let was_present_yesterday = sqlx::query_scalar::<_, bool>(
@@ -126,7 +136,7 @@ async fn update_days_attended(member_id: i32, today: NaiveDate, pool: &PgPool) {
126136
let month: i32 = (today.month0() + 1) as i32;
127137
let year: i32 = today.year_ce().1 as i32;
128138

129-
// Check if theres an existing summary for the current month
139+
// Check if there's an existing summary for the current month
130140
let existing_days_attended = sqlx::query_scalar::<_, i32>(
131141
r#"
132142
SELECT days_attended

0 commit comments

Comments
 (0)