Skip to content

Fix timezone handling in format_date_iso: ensure fallback to UTC for …#1590

Merged
jokob-sk merged 2 commits intomainfrom
next_release
Apr 5, 2026
Merged

Fix timezone handling in format_date_iso: ensure fallback to UTC for …#1590
jokob-sk merged 2 commits intomainfrom
next_release

Conversation

@jokob-sk
Copy link
Copy Markdown
Collaborator

@jokob-sk jokob-sk commented Apr 5, 2026

…invalid configurations

Summary by CodeRabbit

  • Bug Fixes
    • Date formatting now handles missing or invalid timezone configurations by falling back to UTC, preventing errors when converting stored datetimes. This ensures consistent ISO-formatted timestamps are returned for database datetimes (including timezone-naive values) and avoids failures that could disrupt display or logging.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 5, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4847c9ef-ddd2-49a6-820c-6003b8977917

📥 Commits

Reviewing files that changed from the base of the PR and between 2b3d954 and bda8ca3.

📒 Files selected for processing (1)
  • server/utils/datetime_utils.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • server/utils/datetime_utils.py

📝 Walkthrough

Walkthrough

The format_date_iso() function in server/utils/datetime_utils.py now wraps resolution of conf.tz in a try/except and falls back to datetime.UTC if conf.tz is missing, invalid, or ZoneInfo instantiation fails, preserving the existing UTC-to-target conversion and isoformat output.

Changes

Cohort / File(s) Summary
Timezone Resolution Error Handling
server/utils/datetime_utils.py
Wrapped construction of target_tz (from conf.tz) in a try/except that catches ZoneInfoNotFoundError, ValueError, and TypeError; on failure, target_tz falls back to datetime.UTC. No signature changes; original conversion/return flow unchanged.

Poem

🐰 I hopped through tz configs late at night,
Wrapped failures in a gentle light,
When zones go missing or break apart,
UTC holds steady, a warm rabbit heart,
Safe and snug — time's tucked in tight. ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding timezone handling logic with a UTC fallback in the format_date_iso function, which matches the actual code modifications.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch next_release

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
server/utils/datetime_utils.py (1)

215-218: Narrow the broad exception catch in timezone resolution.

At line 217, except Exception is overly broad and can mask unrelated defects. Since this block only resolves timezone input, catch only the expected failures and keep the UTC fallback behavior.

Proposed refactor
-from zoneinfo import ZoneInfo
+from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
...
-            try:
-                target_tz = conf.tz if isinstance(conf.tz, datetime.tzinfo) else ZoneInfo(conf.tz)
-            except Exception:
-                target_tz = datetime.UTC
+            try:
+                if isinstance(conf.tz, datetime.tzinfo):
+                    target_tz = conf.tz
+                elif isinstance(conf.tz, str) and conf.tz:
+                    target_tz = ZoneInfo(conf.tz)
+                else:
+                    target_tz = datetime.UTC
+            except (AttributeError, TypeError, ZoneInfoNotFoundError):
+                target_tz = datetime.UTC
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@server/utils/datetime_utils.py` around lines 215 - 218, The broad except in
the timezone resolution should be narrowed to only the expected errors when
constructing a ZoneInfo from conf.tz; update the try/except around the target_tz
assignment (the block that uses conf.tz and ZoneInfo(conf.tz)) to catch
zoneinfo.ZoneInfoNotFoundError and other relevant construction errors (e.g.,
ValueError/TypeError) instead of Exception, and keep the existing fallback to
datetime.UTC; ensure you reference ZoneInfoNotFoundError via the zoneinfo module
or import it so the except clause is specific.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@server/utils/datetime_utils.py`:
- Around line 215-218: The broad except in the timezone resolution should be
narrowed to only the expected errors when constructing a ZoneInfo from conf.tz;
update the try/except around the target_tz assignment (the block that uses
conf.tz and ZoneInfo(conf.tz)) to catch zoneinfo.ZoneInfoNotFoundError and other
relevant construction errors (e.g., ValueError/TypeError) instead of Exception,
and keep the existing fallback to datetime.UTC; ensure you reference
ZoneInfoNotFoundError via the zoneinfo module or import it so the except clause
is specific.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 417724a8-5003-4822-82d1-9337a0eccbfa

📥 Commits

Reviewing files that changed from the base of the PR and between c0d2daf and 2b3d954.

📒 Files selected for processing (1)
  • server/utils/datetime_utils.py

@jokob-sk jokob-sk merged commit e1ad574 into main Apr 5, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant