Skip to content

Conversation

@themavik
Copy link

@themavik themavik commented Feb 10, 2026

Summary

datetime.utcnow() has been deprecated since Python 3.12 because it returns a naive datetime object that carries no timezone information, making it error-prone when mixed with timezone-aware datetimes. The recommended replacement is datetime.now(tz=timezone.utc) which returns a proper timezone-aware UTC datetime.

Changes

Replaced all 8 occurrences of datetime.utcnow() across 7 files:

Source files (5)

  • elementary/utils/time.pyget_now_utc_str()
  • elementary/monitor/fetchers/alerts/schema/pending_alerts.py — alert timestamp defaults
  • elementary/monitor/data_monitoring/alerts/data_monitoring_alerts.py — suppression interval comparison
  • elementary/messages/messaging_integrations/teams_webhook.py — message send timestamp
  • elementary/messages/messaging_integrations/slack_webhook.py — message send timestamp
  • elementary/messages/messaging_integrations/file_system.py — filename generation and send timestamp

Test files (2)

  • tests/unit/monitor/fetchers/alerts/schemas/test_alert_data_schema.py
  • tests/mocks/fetchers/alerts_fetcher_mock.py

Behavior

The returned datetime values are identical in absolute terms (both represent "now in UTC"), but datetime.now(tz=timezone.utc) is timezone-aware and won't produce deprecation warnings in Python 3.12+.

Assisted by Claude Opus 4.6 max thinking

Made with Cursor

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Improved timezone handling throughout the application to ensure consistent and accurate UTC timestamp representation across all time-sensitive features.

…ne.utc)

`datetime.utcnow()` has been deprecated since Python 3.12 because it
returns a naive datetime that carries no timezone information, making
it error-prone when mixed with timezone-aware datetimes.

Replace all 8 occurrences across 7 files (5 source, 2 test) with the
recommended `datetime.now(tz=timezone.utc)` which returns a proper
timezone-aware datetime.

Files changed:
- elementary/utils/time.py
- elementary/monitor/fetchers/alerts/schema/pending_alerts.py
- elementary/monitor/data_monitoring/alerts/data_monitoring_alerts.py
- elementary/messages/messaging_integrations/teams_webhook.py
- elementary/messages/messaging_integrations/slack_webhook.py
- elementary/messages/messaging_integrations/file_system.py
- tests/unit/monitor/fetchers/alerts/schemas/test_alert_data_schema.py
- tests/mocks/fetchers/alerts_fetcher_mock.py
@github-actions
Copy link
Contributor

👋 @themavik
Thank you for raising your pull request.
Please make sure to add tests and document all user-facing changes.
You can do this by editing the docs files in this pull request.

@themavik themavik requested a deployment to elementary_test_env February 10, 2026 19:03 — with GitHub Actions Waiting
@coderabbitai
Copy link

coderabbitai bot commented Feb 10, 2026

📝 Walkthrough

Walkthrough

This PR systematically replaces naive UTC timestamps with timezone-aware UTC timestamps across 8 files by importing timezone and substituting datetime.utcnow() calls with datetime.now(tz=timezone.utc).

Changes

Cohort / File(s) Summary
Messaging Integrations
elementary/messages/messaging_integrations/file_system.py, slack_webhook.py, teams_webhook.py
Added timezone import and replaced datetime.utcnow() with datetime.now(tz=timezone.utc) for timezone-aware UTC timestamp generation in filename formatting and message timestamps.
Monitoring & Alerts
elementary/monitor/data_monitoring_alerts.py, elementary/monitor/fetchers/alerts/schema/pending_alerts.py
Imported timezone and changed UTC time retrieval from datetime.utcnow() to datetime.now(tz=timezone.utc) for suppression logic and alert schema timestamp defaults.
Utilities
elementary/utils/time.py
Updated get_now_utc_str() to use datetime.now(timezone.utc) instead of datetime.utcnow() for timezone-aware UTC datetime string generation.
Test Utilities
tests/mocks/fetchers/alerts_fetcher_mock.py, tests/unit/monitor/fetchers/alerts/schemas/test_alert_data_schema.py
Added timezone import and replaced datetime.utcnow() with datetime.now(tz=timezone.utc) for timezone-aware test fixture timestamps.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Hops with glee through timezones aware,
No more naive UTC floating bare!
With timezone-aware stamps so bright,
The datetime dance is now just right!

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: replacing deprecated datetime.utcnow() with timezone-aware datetime.now(tz=timezone.utc) across multiple files.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

No actionable comments were generated in the recent review. 🎉

🧹 Recent nitpick comments
elementary/monitor/data_monitoring/alerts/data_monitoring_alerts.py (1)

204-204: convert_time_to_timezone is now redundant for an already timezone-aware UTC datetime.

datetime.now(tz=timezone.utc) already returns a timezone-aware UTC datetime. Wrapping it in convert_time_to_timezone (with no target timezone) just performs a no-op UTC→UTC conversion. You could simplify to:

-        current_time_utc = convert_time_to_timezone(datetime.now(tz=timezone.utc))
+        current_time_utc = datetime.now(tz=timezone.utc)

This works because last_sent_time on line 214 is still converted independently via convert_time_to_timezone(last_sent_time), and both sides will be UTC-aware for the subtraction.


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.

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