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 <noreply@anthropic.com>
This commit is contained in:
@@ -10,7 +10,7 @@ This module handles:
|
|||||||
|
|
||||||
import json
|
import json
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import datetime, timedelta, time
|
from datetime import datetime, timedelta, time, timezone
|
||||||
from typing import Optional, Dict, List, Tuple
|
from typing import Optional, Dict, List, Tuple
|
||||||
import core.postgres as postgres
|
import core.postgres as postgres
|
||||||
from core.tz import user_now, user_today_for
|
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)
|
nag_interval = settings.get("nag_interval_minutes", 15)
|
||||||
|
|
||||||
if last_nag:
|
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
|
time_since_last_nag = (current_time - last_nag).total_seconds() / 60
|
||||||
if time_since_last_nag < nag_interval:
|
if time_since_last_nag < nag_interval:
|
||||||
return False, f"Too soon ({int(time_since_last_nag)} < {nag_interval} min)"
|
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(
|
postgres.update(
|
||||||
"medication_schedules",
|
"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"]},
|
{"id": schedule["id"]},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user