mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
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.
This commit is contained in:
@@ -5,15 +5,15 @@ from contextlib import contextmanager
|
||||
from unittest import SkipTest, skipIf
|
||||
from xml.dom.minidom import parseString
|
||||
|
||||
import pytz
|
||||
|
||||
try:
|
||||
import zoneinfo
|
||||
except ImportError:
|
||||
try:
|
||||
from backports import zoneinfo
|
||||
except ImportError:
|
||||
zoneinfo = None
|
||||
from backports import zoneinfo
|
||||
|
||||
try:
|
||||
import pytz
|
||||
except ImportError:
|
||||
pytz = None
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.core import serializers
|
||||
@@ -61,9 +61,9 @@ UTC = timezone.utc
|
||||
EAT = timezone.get_fixed_timezone(180) # Africa/Nairobi
|
||||
ICT = timezone.get_fixed_timezone(420) # Asia/Bangkok
|
||||
|
||||
ZONE_CONSTRUCTORS = (pytz.timezone,)
|
||||
if zoneinfo is not None:
|
||||
ZONE_CONSTRUCTORS += (zoneinfo.ZoneInfo,)
|
||||
ZONE_CONSTRUCTORS = (zoneinfo.ZoneInfo,)
|
||||
if pytz is not None:
|
||||
ZONE_CONSTRUCTORS += (pytz.timezone,)
|
||||
|
||||
|
||||
def get_timezones(key):
|
||||
@@ -363,6 +363,23 @@ class NewDatabaseTests(TestCase):
|
||||
self.assertEqual(Event.objects.filter(dt__in=(prev, dt, next)).count(), 1)
|
||||
self.assertEqual(Event.objects.filter(dt__range=(prev, next)).count(), 1)
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango50Warning)
|
||||
def test_connection_timezone(self):
|
||||
tests = [
|
||||
(False, None, datetime.timezone),
|
||||
(False, 'Africa/Nairobi', zoneinfo.ZoneInfo),
|
||||
]
|
||||
if pytz is not None:
|
||||
tests += [
|
||||
(True, None, datetime.timezone),
|
||||
(True, 'Africa/Nairobi', pytz.BaseTzInfo),
|
||||
]
|
||||
for use_pytz, connection_tz, expected_type in tests:
|
||||
with self.subTest(use_pytz=use_pytz, connection_tz=connection_tz):
|
||||
with self.settings(USE_DEPRECATED_PYTZ=use_pytz):
|
||||
with override_database_connection_timezone(connection_tz):
|
||||
self.assertIsInstance(connection.timezone, expected_type)
|
||||
|
||||
def test_query_convert_timezones(self):
|
||||
# Connection timezone is equal to the current timezone, datetime
|
||||
# shouldn't be converted.
|
||||
@@ -921,7 +938,7 @@ class TemplateTests(SimpleTestCase):
|
||||
tpl = Template("{% load tz %}{{ dt|timezone:tz }}")
|
||||
ctx = Context({
|
||||
'dt': datetime.datetime(2011, 9, 1, 13, 20, 30),
|
||||
'tz': pytz.timezone('Europe/Paris'),
|
||||
'tz': tz,
|
||||
})
|
||||
self.assertEqual(tpl.render(ctx), "2011-09-01T12:20:30+02:00")
|
||||
|
||||
@@ -994,11 +1011,15 @@ class TemplateTests(SimpleTestCase):
|
||||
})
|
||||
self.assertEqual(tpl.render(ctx), "2011-09-01T12:20:30+02:00")
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango50Warning)
|
||||
def test_timezone_templatetag_invalid_argument(self):
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
Template("{% load tz %}{% timezone %}{% endtimezone %}").render()
|
||||
with self.assertRaises(pytz.UnknownTimeZoneError):
|
||||
with self.assertRaises(zoneinfo.ZoneInfoNotFoundError):
|
||||
Template("{% load tz %}{% timezone tz %}{% endtimezone %}").render(Context({'tz': 'foobar'}))
|
||||
if pytz is not None:
|
||||
with override_settings(USE_DEPRECATED_PYTZ=True), self.assertRaises(pytz.UnknownTimeZoneError):
|
||||
Template("{% load tz %}{% timezone tz %}{% endtimezone %}").render(Context({'tz': 'foobar'}))
|
||||
|
||||
@skipIf(sys.platform == 'win32', "Windows uses non-standard time zone names")
|
||||
def test_get_current_timezone_templatetag(self):
|
||||
|
||||
Reference in New Issue
Block a user