diff --git a/core/adaptive_meds.py b/core/adaptive_meds.py index a7c09b6..252d707 100644 --- a/core/adaptive_meds.py +++ b/core/adaptive_meds.py @@ -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]),