Skip to content

Commit e1ad574

Browse files
authored
Merge pull request #1590 from netalertx/next_release
Fix timezone handling in format_date_iso: ensure fallback to UTC for …
2 parents c0d2daf + bda8ca3 commit e1ad574

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

server/utils/datetime_utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import re
77
import pytz
88
from typing import Union, Optional
9-
from zoneinfo import ZoneInfo
9+
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
1010
import email.utils
1111
import conf
1212
# from const import *
@@ -209,10 +209,13 @@ def format_date_iso(date_val: str) -> Optional[str]:
209209
# 2. If it has no timezone, assume it's UTC (our DB storage format)
210210
# then CONVERT to user's configured timezone
211211
if dt.tzinfo is None:
212-
# Mark as UTC first
212+
# Mark as UTC first — critical: localize() would label without converting
213213
dt = dt.replace(tzinfo=datetime.UTC)
214-
# Convert to user's timezone
215-
target_tz = conf.tz if isinstance(conf.tz, datetime.tzinfo) else ZoneInfo(conf.tz)
214+
# Resolve target timezone; fall back to UTC if conf.tz is missing/invalid
215+
try:
216+
target_tz = conf.tz if isinstance(conf.tz, datetime.tzinfo) else ZoneInfo(conf.tz)
217+
except (ZoneInfoNotFoundError, ValueError, TypeError):
218+
target_tz = datetime.UTC
216219
dt = dt.astimezone(target_tz)
217220

218221
# 3. Return the string. .isoformat() will now include the +11:00 or +10:00

0 commit comments

Comments
 (0)