diff options
author | Carlton Gibson <carlton.gibson@noumenal.es> | 2021-09-09 15:15:44 +0200 |
---|---|---|
committer | Carlton Gibson <carlton.gibson@noumenal.es> | 2021-09-16 12:11:05 +0200 |
commit | 306607d5b99b6eca6ae2c1e726d8eb32b9b2ca1b (patch) | |
tree | 607d1b06feafaf28fc2e09c70652d30659707537 /django/templatetags | |
parent | 7132d17de1399345a38858c20221850bdef43d0e (diff) | |
download | django-306607d5b99b6eca6ae2c1e726d8eb32b9b2ca1b.tar.gz |
Fixed #32365 -- Made zoneinfo the default timezone implementation.
Thanks to Adam Johnson, Aymeric Augustin, David Smith, Mariusz Felisiak, Nick
Pope, and Paul Ganssle for reviews.
Diffstat (limited to 'django/templatetags')
-rw-r--r-- | django/templatetags/tz.py | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/django/templatetags/tz.py b/django/templatetags/tz.py index e1d2fe8507..455c2ed389 100644 --- a/django/templatetags/tz.py +++ b/django/templatetags/tz.py @@ -1,13 +1,37 @@ from datetime import datetime, tzinfo -import pytz +try: + import zoneinfo +except ImportError: + from backports import zoneinfo +from django.conf import settings from django.template import Library, Node, TemplateSyntaxError from django.utils import timezone register = Library() +# RemovedInDjango50Warning: shim to allow catching the exception in the calling +# scope if pytz is not installed. +class UnknownTimezoneException(BaseException): + pass + + +# RemovedInDjango50Warning +def timezone_constructor(tzname): + if settings.USE_DEPRECATED_PYTZ: + import pytz + try: + return pytz.timezone(tzname) + except pytz.UnknownTimeZoneError: + raise UnknownTimezoneException + try: + return zoneinfo.ZoneInfo(tzname) + except zoneinfo.ZoneInfoNotFoundError: + raise UnknownTimezoneException + + # HACK: datetime instances cannot be assigned new attributes. Define a subclass # in order to define new attributes in do_timezone(). class datetimeobject(datetime): @@ -61,8 +85,8 @@ def do_timezone(value, arg): tz = arg elif isinstance(arg, str): try: - tz = pytz.timezone(arg) - except pytz.UnknownTimeZoneError: + tz = timezone_constructor(arg) + except UnknownTimezoneException: return '' else: return '' |