From aeb0b438cc161f6f9b4117702f8b6106a40beab2 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 3 Aug 2026 02:20:57 +0000 Subject: [PATCH] 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 --- diligence/utils/dates.py | 34 +++++++++++++++++++--------------- pyproject.toml | 1 + 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/diligence/utils/dates.py b/diligence/utils/dates.py index d54363f..62b302b 100644 --- a/diligence/utils/dates.py +++ b/diligence/utils/dates.py @@ -9,22 +9,29 @@ from datetime import date, datetime, timezone 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: """Return today's date in the user's timezone.""" - try: - tz = ZoneInfo(tz_str) - except (KeyError, Exception): - tz = ZoneInfo("UTC") - return datetime.now(tz).date() + return datetime.now(_safe_tz(tz_str)).date() def now_for_user(tz_str: str = "UTC") -> datetime: """Return the current datetime in the user's timezone (tz-aware).""" - try: - tz = ZoneInfo(tz_str) - except (KeyError, Exception): - tz = ZoneInfo("UTC") - return datetime.now(tz) + return datetime.now(_safe_tz(tz_str)) 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. """ user_today = today_for_user(tz_str) - try: - tz = ZoneInfo(tz_str) - except (KeyError, Exception): - tz = ZoneInfo("UTC") + tz = _safe_tz(tz_str) 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]: diff --git a/pyproject.toml b/pyproject.toml index b9990c0..fcd4f04 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,7 @@ dependencies = [ "bcrypt>=4.2.0", "httpx>=0.27.0", "python-multipart>=0.0.18", + "tzdata>=2024.1", ] [project.optional-dependencies]