Skip to content

Commit 83de79b

Browse files
committed
Merge branch 'main' of github.com:netalertx/NetAlertX
2 parents 92f1508 + e1ad574 commit 83de79b

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

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)