fix: replace deprecated datetime.utcnow() with datetime.now(tz=timezone.utc)
#2111
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 isdatetime.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.py—get_now_utc_str()elementary/monitor/fetchers/alerts/schema/pending_alerts.py— alert timestamp defaultselementary/monitor/data_monitoring/alerts/data_monitoring_alerts.py— suppression interval comparisonelementary/messages/messaging_integrations/teams_webhook.py— message send timestampelementary/messages/messaging_integrations/slack_webhook.py— message send timestampelementary/messages/messaging_integrations/file_system.py— filename generation and send timestampTest files (2)
tests/unit/monitor/fetchers/alerts/schemas/test_alert_data_schema.pytests/mocks/fetchers/alerts_fetcher_mock.pyBehavior
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