From e9a2f96f917119259adfa9611d0c64d7e528d5a6 Mon Sep 17 00:00:00 2001 From: chelsea Date: Thu, 19 Feb 2026 19:43:32 -0600 Subject: [PATCH] Fix offset-naive/aware datetime error in nag checker Make last_nag_at timezone-aware before subtracting from the tz-aware current_time, and store future nag timestamps with timezone.utc to prevent the mismatch recurring. Co-Authored-By: Claude Sonnet 4.6 --- core/adaptive_meds.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/adaptive_meds.py b/core/adaptive_meds.py index 0b5f646..9364f65 100644 --- a/core/adaptive_meds.py +++ b/core/adaptive_meds.py @@ -10,7 +10,7 @@ This module handles: import json import uuid -from datetime import datetime, timedelta, time +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 @@ -291,6 +291,8 @@ def should_send_nag( nag_interval = settings.get("nag_interval_minutes", 15) if last_nag: + if isinstance(last_nag, datetime) and last_nag.tzinfo is None: + last_nag = last_nag.replace(tzinfo=timezone.utc) time_since_last_nag = (current_time - last_nag).total_seconds() / 60 if time_since_last_nag < nag_interval: return False, f"Too soon ({int(time_since_last_nag)} < {nag_interval} min)" @@ -335,7 +337,7 @@ def record_nag_sent(user_uuid: str, med_id: str, scheduled_time): postgres.update( "medication_schedules", - {"nag_count": new_nag_count, "last_nag_at": datetime.utcnow()}, + {"nag_count": new_nag_count, "last_nag_at": datetime.now(timezone.utc)}, {"id": schedule["id"]}, )