Fix timezone mismatch in already-taken check

This commit is contained in:
2026-02-19 20:51:44 -06:00
parent cf29d17183
commit 03da0b0156

View File

@@ -13,7 +13,7 @@ import uuid
from datetime import datetime, timedelta, time, timezone
from typing import Optional, Dict, List, Tuple
import core.postgres as postgres
from core.tz import user_now, user_today_for
from core.tz import user_now, user_today_for, tz_for_user
def _normalize_time(val):
@@ -328,6 +328,7 @@ def should_send_nag(
proximity_window = max(30, dose_interval_minutes // 2)
# Filter to today's logs and check for this specific dose
user_tz = tz_for_user(user_uuid)
for log in logs:
action = log.get("action")
if action not in ("taken", "skipped"):
@@ -336,7 +337,13 @@ def should_send_nag(
created_at = log.get("created_at")
if not created_at:
continue
if created_at.date() != today:
# created_at is stored as UTC but timezone-naive; convert to user's timezone
if created_at.tzinfo is None:
created_at = created_at.replace(tzinfo=timezone.utc)
created_at_local = created_at.astimezone(user_tz)
if created_at_local.date() != today:
continue
log_scheduled_time = log.get("scheduled_time")
@@ -345,9 +352,9 @@ def should_send_nag(
if log_scheduled_time == scheduled_time:
return False, f"Already {action} today"
else:
if scheduled_time and created_at:
log_hour = created_at.hour
log_min = created_at.minute
if scheduled_time:
log_hour = created_at_local.hour
log_min = created_at_local.minute
sched_hour, sched_min = (
int(scheduled_time[:2]),
int(scheduled_time[3:5]),