-
Notifications
You must be signed in to change notification settings - Fork 4.2k
chore: use zoneinfo instead of pytz #37523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: use zoneinfo instead of pytz #37523
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR replaces the deprecated pytz library with Python's standard zoneinfo module across the OpenEdx platform codebase. As Django 5.0 has completely removed pytz support, this change migrates all timezone handling to use ZoneInfo("UTC") instead of pytz.UTC or pytz.utc, along with converting timezone list utilities from pytz.common_timezones to zoneinfo.available_timezones().
Key changes:
- Replaced
pytz.UTCandpytz.utcimports withZoneInfo("UTC")for UTC timezone handling - Migrated timezone enumeration from
pytz.common_timezonestozoneinfo.available_timezones() - Updated time-related factory fixtures and datetime manipulation code
- Modified timezone utility functions and their associated tests
Reviewed Changes
Copilot reviewed 78 out of 78 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| Multiple test files | Updated timezone references in test fixtures and assertions from pytz to zoneinfo |
| openedx/core/lib/time_zone_utils.py | Core timezone utilities migrated from pytz to zoneinfo for display and enumeration |
| openedx/core/djangoapps/user_api/preferences/api.py | Added compatibility layer for country_timezones fallback while migrating to zoneinfo |
| Multiple views and models | Updated UTC datetime generation from pytz.UTC to ZoneInfo("UTC") |
| Multiple management commands | Updated command date parsing and timestamp generation to use zoneinfo |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
openedx/core/djangoapps/notifications/grouping_notifications.py
Outdated
Show resolved
Hide resolved
robrap
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ttak-apphelix.
Here are some high level comments:
- Have you read https://docs.djangoproject.com/en/5.2/topics/i18n/timezones/? I don't imagine we'll want to switch to Django's timezone, but you should be aware of the docs and warnings.
- I would make a PR that only has direct replacements of [pytz.]UTC => ZoneInfo("UTC"). At a minimum keep this to a separate commit, but a separate PR would make things even simpler. Then, other changes like
utc.localizecan be discussed with fewer distractions, and it would be easier to see and test these unique cases. - The
!in the title means "backward-incompatible" or "breaking change". Can you explain why this is a breaking change? - I'd use
fixup! <COMMIT TITLE>vsfix: <COMMIT TITLE>for commits you plan to squash, and don't intend to leave separate upon merging. That will differentiate from PRs where you intentionally have multiple commits with prefixes that you want to be merged as-is with rebase or merge (vs squash).
Thanks @robrap
|
robrap
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Minor comments.
|
@ttak-apphelix: Could you craft a short commit message, and probably an updated PR description as well, for what we are doing here? This is now not really about the Django changes, but more about moving toward retiring |
@robrap Below are the requested details. Let me know if this is fine: Commit message: Updated PR description: |
|
Thanks @ttak-apphelix. Looks pretty good. I've looked at the issue again: #33980. Some thoughts and questions:
|
@robrap No, zoneinfo.UTC does not exist in the standard library. Right way to use is ZoneInfo("UTC") |
|
@ttak-apphelix: You can see my final commit message here: 18d5abb. In the title I called it "Part 1", because my understanding is that the replacement was not yet done across the whole codebase, but we are doing this in chunks. |
…37523) First PR to replace pytz with zoneinfo for UTC handling across codebase. This PR migrates all UTC timezone handling from pytz to Python’s standard library zoneinfo. The pytz library is now deprecated, and its documentation recommends using zoneinfo for all new code. This update modernizes our codebase, removes legacy pytz usage, and ensures compatibility with current best practices for timezone management in Python 3.9+. No functional changes to timezone logic - just a direct replacement for UTC handling. openedx#33980
Description
pytz got deprecated in Django 4.0 && has been completely removed in Django 5.0.
Django 4.2 had provided USE_DEPRECATED_PYTZ flag for pytz support which has now been completely removed in Django 5.0 as well.
Django now uses zoneinfo by default and datetime module use this under the hood now instead of pytz
Difference between datetime.timezone and zoneinfo
datetime.timezone and the zoneinfo package are both related to handling time zones in Python, but they serve slightly different purposes and have different use cases.
datetime.timezone
This is part of the standard library in Python.
It provides a simple way to represent a fixed offset from UTC (Coordinated Universal Time).
It doesn't have information about daylight saving time (DST) or historical changes in time zones.
It's suitable for scenarios where you only need to work with a constant offset, and historical changes in time zones are not important.
zoneinfo
The zoneinfo package is introduced in Python 3.9 as part of PEP 615.
It provides a more comprehensive and accurate way to handle time zones by including historical changes, daylight saving time transitions, and more.
It uses the IANA Time Zone Database, which is regularly updated to reflect changes in time zones around the world.
This package is suitable for applications that require precise handling of time zones, especially when dealing with historical dates.
Original Issue link:
#33980
Related PR:
#37148
Since it involves multiple files changes and as suggested in the PR planning to break that PR into multiple small PR.
Smaller PR link: