2

pytz is used in the Django version: <=3.2 doc Selecting the current time zone as shown below:

import pytz # Here

from django.utils import timezone

class TimezoneMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        tzname = request.session.get('django_timezone')
        if tzname:
            timezone.activate(pytz.timezone(tzname))
        else:
            timezone.deactivate()
        return self.get_response(request)

But, zoneinfo is used instead of pytz in the Django version: 4.0<= doc Selecting the current time zone as shown below:

import zoneinfo # Here

from django.utils import timezone

class TimezoneMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        tzname = request.session.get('django_timezone')
        if tzname:
            timezone.activate(zoneinfo.ZoneInfo(tzname))
        else:
            timezone.deactivate()
        return self.get_response(request)

My questions:

  1. Is pytz deprecated now or in the future in Python?
  2. Why isn't pytz used in the Django version: 4.0<= doc Selecting the current time zone?
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
  • To quote pytz's readme: *"This project is in maintenance mode. Projects using Python 3.9 or later are best served by using the timezone functionaly now included in core Python and packages that work with it such as tzdata"* https://pypi.org/project/pytz/ – slothrop Jun 29 '23 at 17:00
  • @slothrop I checked `https://github.com/stub42/pytz` but I couldn't find `This project is in maintenance mode. ...`. – Super Kai - Kazuya Ito Jun 29 '23 at 17:09
  • 1
    https://github.com/stub42/pytz/blob/master/src/README.rst, under "Issues & Limitations". – slothrop Jun 29 '23 at 17:11
  • 2
    @slothrop I think that would be good to write up as an answer – juanpa.arrivillaga Jun 29 '23 at 17:16

1 Answers1

3

To quote pytz's README:

This project is in maintenance mode. Projects using Python 3.9 or later are best served by using the timezone functionaly now included in core Python and packages that work with it such as tzdata.

Without using the literal word "deprecated", the package maintainer is proposing that you don't use pytz for new projects, so that's really deprecation for all intents and purposes.

So Django's strategy is consistent with that, and in fact Django goes a bit further by facilitating the use of zoneinfo in Python 3.8 (the oldest Python version still supported). From the Django 4.0 documentation you cited:

Time zone support uses zoneinfo, which is part of the Python standard library from Python 3.9. The backports.zoneinfo package is automatically installed alongside Django if you are using Python 3.8.

and

Changed in Django 4.0: zoneinfo was made the default timezone implementation. You may continue to use pytz during the 4.x release cycle via the USE_DEPRECATED_PYTZ setting.

slothrop
  • 3,218
  • 1
  • 18
  • 11