Ensure consistent UTC timezone handling for timestamps#848
Open
Ensure consistent UTC timezone handling for timestamps#848
Conversation
…modules for consistency
…irth handling in tests
There was a problem hiding this comment.
Pull request overview
Enables Ruff’s DTZ (datetime timezone safety) rules and updates code/tests to consistently use UTC-aware datetimes, including fixing an actual bug where a .replace(tzinfo=...) result was previously discarded.
Changes:
- Enable Ruff
DTZruleset to enforce timezone-aware datetime usage. - Update production code to create/handle UTC-aware timestamps (e.g.,
fromtimestamp(..., tz=UTC),utc_now(),tzinfo=UTC). - Update tests to avoid naive
datetimeconstructors and use UTC-aware timestamps.
Reviewed changes
Copilot reviewed 22 out of 22 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/eduid/webapp/svipe_id/tests/test_app.py | Replace naive datetime.today() usage with utc_now() in test data. |
| src/eduid/webapp/svipe_id/proofing.py | Ensure date_of_birth datetimes are created as UTC-aware. |
| src/eduid/webapp/security/tests/test_app.py | Make test timestamps UTC-aware by adding tzinfo=UTC. |
| src/eduid/webapp/lookup_mobile_proofing/tests/test_app.py | Use utc_now() instead of naive datetime.now() for underage test setup. |
| src/eduid/webapp/lookup_mobile_proofing/helpers.py | Make parsed NIN birthdate UTC-aware for timezone-safe age calculations. |
| src/eduid/webapp/letter_proofing/helpers.py | Make sentinel fromtimestamp(0) UTC-aware. |
| src/eduid/webapp/idp/tests/test_idPUserDb.py | Use utc_now() for “three years ago” timestamp in tests. |
| src/eduid/webapp/idp/tests/test_SSO.py | Make date_of_birth test identities UTC-aware. |
| src/eduid/webapp/freja_eid/tests/test_app.py | Replace naive today/now usages in tests with utc_now(). |
| src/eduid/webapp/freja_eid/proofing.py | Ensure fromtimestamp and date_of_birth handling is UTC-aware. |
| src/eduid/webapp/email/tests/test_app.py | Replace naive datetime.now(tz=None) in tests with utc_now(). |
| src/eduid/webapp/eidas/tests/test_app.py | Ensure parsed date_of_birth is UTC-aware in test auth response generation. |
| src/eduid/webapp/eidas/proofing.py | Ensure date_of_birth datetimes are created as UTC-aware. |
| src/eduid/webapp/common/api/oidc.py | Use utc_now() for circuit breaker timing instead of naive datetime.now(). |
| src/eduid/webapp/bankid/tests/test_app.py | Ensure parsed date_of_birth is UTC-aware in test auth response generation. |
| src/eduid/userdb/tests/test_user.py | Make proofing timestamps in test fixtures UTC-aware. |
| src/eduid/userdb/tests/test_proofing.py | Use utc_now() for proofing state timestamps in tests. |
| src/eduid/userdb/tests/test_event.py | Make event timestamps UTC-aware and update assertions accordingly. |
| src/eduid/userdb/identity.py | Ensure parsed NIN-derived date_of_birth is returned as UTC-aware. |
| src/eduid/userdb/admin/init.py | Add UTC timezone to CLI tool timestamps used for backup paths. |
| src/eduid/graphdb/helpers.py | Fix bug by using fromtimestamp(..., tz=UTC) so Neo4j timestamps become UTC-aware. |
| ruff.toml | Add DTZ to Ruff lint selection. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.


Enable DTZ ruff rules — datetime timezone safety
Summary
Enable the DTZ rule set in ruff to enforce timezone-aware datetime usage across the codebase. Fixes 39 issues, including a real bug in
graphdb/helpers.py.Bug fix
graphdb/helpers.py— discarded.replace()resultdatetime.replace()returns a new object; the result was being discarded, leaving timestamps naive despite the developer's intent:Changes
Production code (18 issues)
graphdb/helpers.pyfromtimestamp()without tz + discarded.replace()tz=UTC, remove.replace()webapp/common/api/oidc.pydatetime.now()in circuit breakerutc_now()webapp/freja_eid/proofing.pyfromtimestamp()+ naivedate_of_birthconstructorstz=UTC/tzinfo=UTCwebapp/eidas/proofing.pydate_of_birthconstructorstzinfo=UTCwebapp/svipe_id/proofing.pydate_of_birthconstructorstzinfo=UTCwebapp/letter_proofing/helpers.pyfromtimestamp(0)sentinel valuetz=UTCuserdb/admin/__init__.pyfromtimestamp()in CLI tooltz=UTCuserdb/identity.pystrptime()for NIN date parsing.replace(tzinfo=UTC)webapp/lookup_mobile_proofing/helpers.pystrptime()for age calc.replace(tzinfo=UTC)Test code (21 issues)
Mechanical fixes — replaced
datetime.now(tz=None)/datetime.today()/ naivedatetime()constructors withutc_now()or addedtzinfo=UTC.Ruff config
Added
"DTZ"to theselectlist inruff.toml.date_of_birthsafety analysisAdding
tzinfo=UTCto the 6 naivedatetime()constructors in proofing code is safe:tz_aware=True— the pymongo connection already interprets naive datetimes as UTC on write and returns them as UTC-aware on read. The stored value is identical with or withouttzinfo=UTC.date_of_birthcomparisons use.date()(which strips timezone) or.strftime()(timezone-agnostic). No direct</>between naive and aware datetimes exists.date_of_birth.