Fix Windows crash: add tzdata dependency, harden timezone fallback

On Windows, Python's zoneinfo module has no system timezone database.
Without the tzdata package, ZoneInfo('UTC') raises ZoneInfoNotFoundError,
crashing both /api/points/today and /api/nutrition/today (Home and Keto
pages show 'failed to load').

Two fixes:
1. Add tzdata>=2024.1 to pyproject.toml dependencies so pip install
   pulls it automatically
2. Harden dates.py with _safe_tz() that falls back to datetime.timezone.utc
   (always available, no tzdata needed) as the ultimate fallback
This commit is contained in:
Claude 2026-08-03 02:20:57 +00:00
parent 3beca1ca2f
commit aeb0b438cc
2 changed files with 20 additions and 15 deletions

View file

@ -9,22 +9,29 @@ from datetime import date, datetime, timezone
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
def _safe_tz(tz_str: str = "UTC"):
"""Resolve a timezone string to a tzinfo, falling back to UTC.
Uses datetime.timezone.utc as the ultimate fallback in case
tzdata is not installed (common on Windows).
"""
try:
return ZoneInfo(tz_str)
except Exception:
try:
return ZoneInfo("UTC")
except Exception:
return timezone.utc
def today_for_user(tz_str: str = "UTC") -> date: def today_for_user(tz_str: str = "UTC") -> date:
"""Return today's date in the user's timezone.""" """Return today's date in the user's timezone."""
try: return datetime.now(_safe_tz(tz_str)).date()
tz = ZoneInfo(tz_str)
except (KeyError, Exception):
tz = ZoneInfo("UTC")
return datetime.now(tz).date()
def now_for_user(tz_str: str = "UTC") -> datetime: def now_for_user(tz_str: str = "UTC") -> datetime:
"""Return the current datetime in the user's timezone (tz-aware).""" """Return the current datetime in the user's timezone (tz-aware)."""
try: return datetime.now(_safe_tz(tz_str))
tz = ZoneInfo(tz_str)
except (KeyError, Exception):
tz = ZoneInfo("UTC")
return datetime.now(tz)
def day_start_utc(tz_str: str = "UTC") -> datetime: def day_start_utc(tz_str: str = "UTC") -> datetime:
@ -34,12 +41,9 @@ def day_start_utc(tz_str: str = "UTC") -> datetime:
to filter by the user's local day. to filter by the user's local day.
""" """
user_today = today_for_user(tz_str) user_today = today_for_user(tz_str)
try: tz = _safe_tz(tz_str)
tz = ZoneInfo(tz_str)
except (KeyError, Exception):
tz = ZoneInfo("UTC")
local_midnight = datetime(user_today.year, user_today.month, user_today.day, tzinfo=tz) local_midnight = datetime(user_today.year, user_today.month, user_today.day, tzinfo=tz)
return local_midnight.astimezone(ZoneInfo("UTC")) return local_midnight.astimezone(timezone.utc)
def get_week_boundaries(d: date | None = None, tz_str: str = "UTC") -> tuple[date, date]: def get_week_boundaries(d: date | None = None, tz_str: str = "UTC") -> tuple[date, date]:

View file

@ -34,6 +34,7 @@ dependencies = [
"bcrypt>=4.2.0", "bcrypt>=4.2.0",
"httpx>=0.27.0", "httpx>=0.27.0",
"python-multipart>=0.0.18", "python-multipart>=0.0.18",
"tzdata>=2024.1",
] ]
[project.optional-dependencies] [project.optional-dependencies]