Refs #32365 -- Allowed use of non-pytz timezone implementations.
This commit is contained in:
parent
73ffc73b68
commit
10d1261984
|
@ -161,6 +161,11 @@ def from_current_timezone(value):
|
|||
if settings.USE_TZ and value is not None and timezone.is_naive(value):
|
||||
current_timezone = timezone.get_current_timezone()
|
||||
try:
|
||||
if (
|
||||
not timezone._is_pytz_zone(current_timezone) and
|
||||
timezone._datetime_ambiguous_or_imaginary(value, current_timezone)
|
||||
):
|
||||
raise ValueError('Ambiguous or non-existent time.')
|
||||
return timezone.make_aware(value, current_timezone)
|
||||
except Exception as exc:
|
||||
raise ValidationError(
|
||||
|
|
|
@ -20,7 +20,8 @@ from django.utils.dates import (
|
|||
)
|
||||
from django.utils.regex_helper import _lazy_re_compile
|
||||
from django.utils.timezone import (
|
||||
get_default_timezone, is_aware, is_naive, make_aware,
|
||||
_datetime_ambiguous_or_imaginary, get_default_timezone, is_aware, is_naive,
|
||||
make_aware,
|
||||
)
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
|
@ -160,15 +161,9 @@ class TimeFormat(Formatter):
|
|||
if not self.timezone:
|
||||
return ""
|
||||
|
||||
name = None
|
||||
try:
|
||||
if not _datetime_ambiguous_or_imaginary(self.data, self.timezone):
|
||||
name = self.timezone.tzname(self.data)
|
||||
except Exception:
|
||||
# pytz raises AmbiguousTimeError during the autumn DST change.
|
||||
# This happens mainly when __init__ receives a naive datetime
|
||||
# and sets self.timezone = get_default_timezone().
|
||||
pass
|
||||
if name is None:
|
||||
else:
|
||||
name = self.format('O')
|
||||
return str(name)
|
||||
|
||||
|
@ -184,16 +179,13 @@ class TimeFormat(Formatter):
|
|||
|
||||
If timezone information is not available, return an empty string.
|
||||
"""
|
||||
if not self.timezone:
|
||||
if (
|
||||
not self.timezone or
|
||||
_datetime_ambiguous_or_imaginary(self.data, self.timezone)
|
||||
):
|
||||
return ""
|
||||
|
||||
try:
|
||||
offset = self.timezone.utcoffset(self.data)
|
||||
except Exception:
|
||||
# pytz raises AmbiguousTimeError during the autumn DST change.
|
||||
# This happens mainly when __init__ receives a naive datetime
|
||||
# and sets self.timezone = get_default_timezone().
|
||||
return ""
|
||||
|
||||
# `offset` is a datetime.timedelta. For negative values (to the west of
|
||||
# UTC) only days can be negative (days=-1) and seconds are always
|
||||
|
@ -232,16 +224,12 @@ class DateFormat(TimeFormat):
|
|||
|
||||
def I(self): # NOQA: E743, E741
|
||||
"'1' if Daylight Savings Time, '0' otherwise."
|
||||
try:
|
||||
if self.timezone and self.timezone.dst(self.data):
|
||||
return '1'
|
||||
else:
|
||||
return '0'
|
||||
except Exception:
|
||||
# pytz raises AmbiguousTimeError during the autumn DST change.
|
||||
# This happens mainly when __init__ receives a naive datetime
|
||||
# and sets self.timezone = get_default_timezone().
|
||||
if (
|
||||
not self.timezone or
|
||||
_datetime_ambiguous_or_imaginary(self.data, self.timezone)
|
||||
):
|
||||
return ''
|
||||
return '1' if self.timezone.dst(self.data) else '0'
|
||||
|
||||
def j(self):
|
||||
"Day of the month without leading zeros; i.e. '1' to '31'"
|
||||
|
|
|
@ -24,6 +24,11 @@ __all__ = [
|
|||
# UTC time zone as a tzinfo instance.
|
||||
utc = pytz.utc
|
||||
|
||||
_PYTZ_BASE_CLASSES = (pytz.tzinfo.BaseTzInfo, pytz._FixedOffset)
|
||||
# In releases prior to 2018.4, pytz.UTC was not a subclass of BaseTzInfo
|
||||
if not isinstance(pytz.UTC, pytz._FixedOffset):
|
||||
_PYTZ_BASE_CLASSES = _PYTZ_BASE_CLASSES + (type(pytz.UTC),)
|
||||
|
||||
|
||||
def get_fixed_timezone(offset):
|
||||
"""Return a tzinfo instance with a fixed offset from UTC."""
|
||||
|
@ -68,7 +73,7 @@ def get_current_timezone_name():
|
|||
|
||||
def _get_timezone_name(timezone):
|
||||
"""Return the name of ``timezone``."""
|
||||
return timezone.tzname(None)
|
||||
return str(timezone)
|
||||
|
||||
# Timezone selection functions.
|
||||
|
||||
|
@ -229,7 +234,7 @@ def make_aware(value, timezone=None, is_dst=None):
|
|||
"""Make a naive datetime.datetime in a given time zone aware."""
|
||||
if timezone is None:
|
||||
timezone = get_current_timezone()
|
||||
if hasattr(timezone, 'localize'):
|
||||
if _is_pytz_zone(timezone):
|
||||
# This method is available for pytz time zones.
|
||||
return timezone.localize(value, is_dst=is_dst)
|
||||
else:
|
||||
|
@ -249,3 +254,20 @@ def make_naive(value, timezone=None):
|
|||
if is_naive(value):
|
||||
raise ValueError("make_naive() cannot be applied to a naive datetime")
|
||||
return value.astimezone(timezone).replace(tzinfo=None)
|
||||
|
||||
|
||||
def _is_pytz_zone(tz):
|
||||
"""Checks if a zone is a pytz zone."""
|
||||
return isinstance(tz, _PYTZ_BASE_CLASSES)
|
||||
|
||||
|
||||
def _datetime_ambiguous_or_imaginary(dt, tz):
|
||||
if _is_pytz_zone(tz):
|
||||
try:
|
||||
tz.utcoffset(dt)
|
||||
except (pytz.AmbiguousTimeError, pytz.NonExistentTimeError):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
return tz.utcoffset(dt.replace(fold=not dt.fold)) != tz.utcoffset(dt)
|
||||
|
|
|
@ -943,21 +943,24 @@ appropriate entities.
|
|||
:class:`~datetime.datetime`. If ``timezone`` is set to ``None``, it
|
||||
defaults to the :ref:`current time zone <default-current-time-zone>`.
|
||||
|
||||
The ``pytz.AmbiguousTimeError`` exception is raised if you try to make
|
||||
``value`` aware during a DST transition where the same time occurs twice
|
||||
(when reverting from DST). Setting ``is_dst`` to ``True`` or ``False`` will
|
||||
avoid the exception by choosing if the time is pre-transition or
|
||||
post-transition respectively.
|
||||
When using ``pytz``, the ``pytz.AmbiguousTimeError`` exception is raised if
|
||||
you try to make ``value`` aware during a DST transition where the same time
|
||||
occurs twice (when reverting from DST). Setting ``is_dst`` to ``True`` or
|
||||
``False`` will avoid the exception by choosing if the time is
|
||||
pre-transition or post-transition respectively.
|
||||
|
||||
The ``pytz.NonExistentTimeError`` exception is raised if you try to make
|
||||
``value`` aware during a DST transition such that the time never occurred.
|
||||
For example, if the 2:00 hour is skipped during a DST transition, trying to
|
||||
make 2:30 aware in that time zone will raise an exception. To avoid that
|
||||
you can use ``is_dst`` to specify how ``make_aware()`` should interpret
|
||||
such a nonexistent time. If ``is_dst=True`` then the above time would be
|
||||
interpreted as 2:30 DST time (equivalent to 1:30 local time). Conversely,
|
||||
if ``is_dst=False`` the time would be interpreted as 2:30 standard time
|
||||
(equivalent to 3:30 local time).
|
||||
When using ``pytz``, the ``pytz.NonExistentTimeError`` exception is raised
|
||||
if you try to make ``value`` aware during a DST transition such that the
|
||||
time never occurred. For example, if the 2:00 hour is skipped during a DST
|
||||
transition, trying to make 2:30 aware in that time zone will raise an
|
||||
exception. To avoid that you can use ``is_dst`` to specify how
|
||||
``make_aware()`` should interpret such a nonexistent time. If
|
||||
``is_dst=True`` then the above time would be interpreted as 2:30 DST time
|
||||
(equivalent to 1:30 local time). Conversely, if ``is_dst=False`` the time
|
||||
would be interpreted as 2:30 standard time (equivalent to 3:30 local time).
|
||||
|
||||
The ``is_dst`` parameter has no effect when using non-``pytz`` timezone
|
||||
implementations.
|
||||
|
||||
.. function:: make_naive(value, timezone=None)
|
||||
|
||||
|
|
|
@ -657,6 +657,9 @@ MySQL 5.7 and higher.
|
|||
Miscellaneous
|
||||
-------------
|
||||
|
||||
* Django now supports non-``pytz`` time zones, such as Python 3.9+'s
|
||||
:mod:`zoneinfo` module and its backport.
|
||||
|
||||
* The undocumented ``SpatiaLiteOperations.proj4_version()`` method is renamed
|
||||
to ``proj_version()``.
|
||||
|
||||
|
|
|
@ -26,8 +26,15 @@ to this problem is to use UTC in the code and use local time only when
|
|||
interacting with end users.
|
||||
|
||||
Time zone support is disabled by default. To enable it, set :setting:`USE_TZ =
|
||||
True <USE_TZ>` in your settings file. Time zone support uses pytz_, which is
|
||||
installed when you install Django.
|
||||
True <USE_TZ>` in your settings file. By default, time zone support uses pytz_,
|
||||
which is installed when you install Django; Django also supports the use of
|
||||
other time zone implementations like :mod:`zoneinfo` by passing
|
||||
:class:`~datetime.tzinfo` objects directly to functions in
|
||||
:mod:`django.utils.timezone`.
|
||||
|
||||
.. versionchanged:: 3.2
|
||||
|
||||
Support for non-``pytz`` timezone implementations was added.
|
||||
|
||||
.. note::
|
||||
|
||||
|
@ -680,7 +687,8 @@ Usage
|
|||
|
||||
pytz_ provides helpers_, including a list of current time zones and a list
|
||||
of all available time zones -- some of which are only of historical
|
||||
interest.
|
||||
interest. :mod:`zoneinfo` also provides similar functionality via
|
||||
:func:`zoneinfo.available_timezones`.
|
||||
|
||||
.. _pytz: http://pytz.sourceforge.net/
|
||||
.. _more examples: http://pytz.sourceforge.net/#example-usage
|
||||
|
|
|
@ -7,6 +7,14 @@ from urllib.parse import parse_qsl, urljoin, urlparse
|
|||
|
||||
import pytz
|
||||
|
||||
try:
|
||||
import zoneinfo
|
||||
except ImportError:
|
||||
try:
|
||||
from backports import zoneinfo
|
||||
except ImportError:
|
||||
zoneinfo = None
|
||||
|
||||
from django.contrib import admin
|
||||
from django.contrib.admin import AdminSite, ModelAdmin
|
||||
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
|
||||
|
@ -63,6 +71,14 @@ for a staff account. Note that both fields may be case-sensitive."
|
|||
MULTIPART_ENCTYPE = 'enctype="multipart/form-data"'
|
||||
|
||||
|
||||
def make_aware_datetimes(dt, iana_key):
|
||||
"""Makes one aware datetime for each supported time zone provider."""
|
||||
yield pytz.timezone(iana_key).localize(dt, is_dst=None)
|
||||
|
||||
if zoneinfo is not None:
|
||||
yield dt.replace(tzinfo=zoneinfo.ZoneInfo(iana_key))
|
||||
|
||||
|
||||
class AdminFieldExtractionMixin:
|
||||
"""
|
||||
Helper methods for extracting data from AdminForm.
|
||||
|
@ -995,7 +1011,8 @@ class AdminViewBasicTest(AdminViewBasicTestCase):
|
|||
@override_settings(TIME_ZONE='America/Sao_Paulo', USE_TZ=True)
|
||||
def test_date_hierarchy_timezone_dst(self):
|
||||
# This datetime doesn't exist in this timezone due to DST.
|
||||
date = pytz.timezone('America/Sao_Paulo').localize(datetime.datetime(2016, 10, 16, 15), is_dst=None)
|
||||
for date in make_aware_datetimes(datetime.datetime(2016, 10, 16, 15), 'America/Sao_Paulo'):
|
||||
with self.subTest(repr(date.tzinfo)):
|
||||
q = Question.objects.create(question='Why?', expires=date)
|
||||
Answer2.objects.create(question=q, answer='Because.')
|
||||
response = self.client.get(reverse('admin:admin_views_answer2_changelist'))
|
||||
|
@ -1006,7 +1023,8 @@ class AdminViewBasicTest(AdminViewBasicTestCase):
|
|||
@override_settings(TIME_ZONE='America/Los_Angeles', USE_TZ=True)
|
||||
def test_date_hierarchy_local_date_differ_from_utc(self):
|
||||
# This datetime is 2017-01-01 in UTC.
|
||||
date = pytz.timezone('America/Los_Angeles').localize(datetime.datetime(2016, 12, 31, 16))
|
||||
for date in make_aware_datetimes(datetime.datetime(2016, 12, 31, 16), 'America/Los_Angeles'):
|
||||
with self.subTest(repr(date.tzinfo)):
|
||||
q = Question.objects.create(question='Why?', expires=date)
|
||||
Answer2.objects.create(question=q, answer='Because.')
|
||||
response = self.client.get(reverse('admin:admin_views_answer2_changelist'))
|
||||
|
|
|
@ -2,6 +2,14 @@ from datetime import datetime, timedelta, timezone as datetime_timezone
|
|||
|
||||
import pytz
|
||||
|
||||
try:
|
||||
import zoneinfo
|
||||
except ImportError:
|
||||
try:
|
||||
from backports import zoneinfo
|
||||
except ImportError:
|
||||
zoneinfo = None
|
||||
|
||||
from django.conf import settings
|
||||
from django.db.models import (
|
||||
DateField, DateTimeField, F, IntegerField, Max, OuterRef, Subquery,
|
||||
|
@ -21,6 +29,10 @@ from django.utils import timezone
|
|||
|
||||
from ..models import Author, DTModel, Fan
|
||||
|
||||
ZONE_CONSTRUCTORS = (pytz.timezone,)
|
||||
if zoneinfo is not None:
|
||||
ZONE_CONSTRUCTORS += (zoneinfo.ZoneInfo,)
|
||||
|
||||
|
||||
def truncate_to(value, kind, tzinfo=None):
|
||||
# Convert to target timezone before truncation
|
||||
|
@ -1039,7 +1051,7 @@ class DateFunctionTests(TestCase):
|
|||
outer = Author.objects.annotate(
|
||||
newest_fan_year=TruncYear(Subquery(inner, output_field=DateTimeField()))
|
||||
)
|
||||
tz = pytz.UTC if settings.USE_TZ else None
|
||||
tz = timezone.utc if settings.USE_TZ else None
|
||||
self.assertSequenceEqual(
|
||||
outer.order_by('name').values('name', 'newest_fan_year'),
|
||||
[
|
||||
|
@ -1052,16 +1064,21 @@ class DateFunctionTests(TestCase):
|
|||
@override_settings(USE_TZ=True, TIME_ZONE='UTC')
|
||||
class DateFunctionWithTimeZoneTests(DateFunctionTests):
|
||||
|
||||
def get_timezones(self, key):
|
||||
for constructor in ZONE_CONSTRUCTORS:
|
||||
yield constructor(key)
|
||||
|
||||
def test_extract_func_with_timezone(self):
|
||||
start_datetime = datetime(2015, 6, 15, 23, 30, 1, 321)
|
||||
end_datetime = datetime(2015, 6, 16, 13, 11, 27, 123)
|
||||
start_datetime = timezone.make_aware(start_datetime, is_dst=False)
|
||||
end_datetime = timezone.make_aware(end_datetime, is_dst=False)
|
||||
self.create_model(start_datetime, end_datetime)
|
||||
melb = pytz.timezone('Australia/Melbourne')
|
||||
delta_tzinfo_pos = datetime_timezone(timedelta(hours=5))
|
||||
delta_tzinfo_neg = datetime_timezone(timedelta(hours=-5, minutes=17))
|
||||
|
||||
for melb in self.get_timezones('Australia/Melbourne'):
|
||||
with self.subTest(repr(melb)):
|
||||
qs = DTModel.objects.annotate(
|
||||
day=Extract('start_datetime', 'day'),
|
||||
day_melb=Extract('start_datetime', 'day', tzinfo=melb),
|
||||
|
@ -1116,8 +1133,9 @@ class DateFunctionWithTimeZoneTests(DateFunctionTests):
|
|||
start_datetime = timezone.make_aware(start_datetime, is_dst=False)
|
||||
end_datetime = timezone.make_aware(end_datetime, is_dst=False)
|
||||
self.create_model(start_datetime, end_datetime)
|
||||
melb = pytz.timezone('Australia/Melbourne')
|
||||
|
||||
for melb in self.get_timezones('Australia/Melbourne'):
|
||||
with self.subTest(repr(melb)):
|
||||
with timezone.override(melb):
|
||||
model = DTModel.objects.annotate(
|
||||
day_melb=Extract('start_datetime', 'day'),
|
||||
|
@ -1127,7 +1145,8 @@ class DateFunctionWithTimeZoneTests(DateFunctionTests):
|
|||
self.assertEqual(model.day_utc, 15)
|
||||
|
||||
def test_extract_invalid_field_with_timezone(self):
|
||||
melb = pytz.timezone('Australia/Melbourne')
|
||||
for melb in self.get_timezones('Australia/Melbourne'):
|
||||
with self.subTest(repr(melb)):
|
||||
msg = 'tzinfo can only be used with DateTimeField.'
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
DTModel.objects.annotate(
|
||||
|
@ -1145,9 +1164,10 @@ class DateFunctionWithTimeZoneTests(DateFunctionTests):
|
|||
end_datetime = timezone.make_aware(end_datetime, is_dst=False)
|
||||
self.create_model(start_datetime, end_datetime)
|
||||
|
||||
melb = pytz.timezone('Australia/Melbourne')
|
||||
pacific = pytz.timezone('US/Pacific')
|
||||
|
||||
for melb, pacific in zip(
|
||||
self.get_timezones('Australia/Melbourne'), self.get_timezones('America/Los_Angeles')
|
||||
):
|
||||
with self.subTest((repr(melb), repr(pacific))):
|
||||
model = DTModel.objects.annotate(
|
||||
melb_year=TruncYear('start_datetime', tzinfo=melb),
|
||||
pacific_year=TruncYear('start_datetime', tzinfo=pacific),
|
||||
|
@ -1172,9 +1192,9 @@ class DateFunctionWithTimeZoneTests(DateFunctionTests):
|
|||
|
||||
def test_trunc_ambiguous_and_invalid_times(self):
|
||||
sao = pytz.timezone('America/Sao_Paulo')
|
||||
utc = pytz.timezone('UTC')
|
||||
start_datetime = utc.localize(datetime(2016, 10, 16, 13))
|
||||
end_datetime = utc.localize(datetime(2016, 2, 21, 1))
|
||||
utc = timezone.utc
|
||||
start_datetime = datetime(2016, 10, 16, 13, tzinfo=utc)
|
||||
end_datetime = datetime(2016, 2, 21, 1, tzinfo=utc)
|
||||
self.create_model(start_datetime, end_datetime)
|
||||
with timezone.override(sao):
|
||||
with self.assertRaisesMessage(pytz.NonExistentTimeError, '2016-10-16 00:00:00'):
|
||||
|
@ -1206,12 +1226,14 @@ class DateFunctionWithTimeZoneTests(DateFunctionTests):
|
|||
self.create_model(start_datetime, end_datetime)
|
||||
self.create_model(end_datetime, start_datetime)
|
||||
|
||||
melb = pytz.timezone('Australia/Melbourne')
|
||||
|
||||
for melb in self.get_timezones('Australia/Melbourne'):
|
||||
with self.subTest(repr(melb)):
|
||||
def test_datetime_kind(kind):
|
||||
self.assertQuerysetEqual(
|
||||
DTModel.objects.annotate(
|
||||
truncated=Trunc('start_datetime', kind, output_field=DateTimeField(), tzinfo=melb)
|
||||
truncated=Trunc(
|
||||
'start_datetime', kind, output_field=DateTimeField(), tzinfo=melb
|
||||
)
|
||||
).order_by('start_datetime'),
|
||||
[
|
||||
(start_datetime, truncate_to(start_datetime.astimezone(melb), kind, melb)),
|
||||
|
@ -1283,11 +1305,14 @@ class DateFunctionWithTimeZoneTests(DateFunctionTests):
|
|||
test_datetime_kind('minute')
|
||||
test_datetime_kind('second')
|
||||
|
||||
qs = DTModel.objects.filter(start_datetime__date=Trunc('start_datetime', 'day', output_field=DateField()))
|
||||
qs = DTModel.objects.filter(
|
||||
start_datetime__date=Trunc('start_datetime', 'day', output_field=DateField())
|
||||
)
|
||||
self.assertEqual(qs.count(), 2)
|
||||
|
||||
def test_trunc_invalid_field_with_timezone(self):
|
||||
melb = pytz.timezone('Australia/Melbourne')
|
||||
for melb in self.get_timezones('Australia/Melbourne'):
|
||||
with self.subTest(repr(melb)):
|
||||
msg = 'tzinfo can only be used with DateTimeField.'
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
DTModel.objects.annotate(
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
asgiref >= 3.2.10
|
||||
argon2-cffi >= 16.1.0
|
||||
backports.zoneinfo; python_version < '3.9'
|
||||
bcrypt
|
||||
docutils
|
||||
geoip2
|
||||
|
@ -17,4 +18,5 @@ PyYAML
|
|||
selenium
|
||||
sqlparse >= 0.2.2
|
||||
tblib >= 1.5.0
|
||||
tzdata
|
||||
colorama; sys.platform == 'win32'
|
||||
|
|
|
@ -7,6 +7,14 @@ from xml.dom.minidom import parseString
|
|||
|
||||
import pytz
|
||||
|
||||
try:
|
||||
import zoneinfo
|
||||
except ImportError:
|
||||
try:
|
||||
from backports import zoneinfo
|
||||
except ImportError:
|
||||
zoneinfo = None
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.core import serializers
|
||||
from django.db import connection
|
||||
|
@ -51,6 +59,14 @@ 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,)
|
||||
|
||||
|
||||
def get_timezones(key):
|
||||
return [constructor(key) for constructor in ZONE_CONSTRUCTORS]
|
||||
|
||||
|
||||
@contextmanager
|
||||
def override_database_connection_timezone(timezone):
|
||||
|
@ -326,7 +342,8 @@ class NewDatabaseTests(TestCase):
|
|||
self.assertEqual(Event.objects.filter(dt__gt=dt2).count(), 0)
|
||||
|
||||
def test_query_filter_with_pytz_timezones(self):
|
||||
tz = pytz.timezone('Europe/Paris')
|
||||
for tz in get_timezones('Europe/Paris'):
|
||||
with self.subTest(repr(tz)):
|
||||
dt = datetime.datetime(2011, 9, 1, 12, 20, 30, tzinfo=tz)
|
||||
Event.objects.create(dt=dt)
|
||||
next = dt + datetime.timedelta(seconds=3)
|
||||
|
@ -543,7 +560,7 @@ class NewDatabaseTests(TestCase):
|
|||
with connection.cursor() as cursor:
|
||||
cursor.execute('SELECT CURRENT_TIMESTAMP')
|
||||
now = cursor.fetchone()[0]
|
||||
self.assertEqual(now.tzinfo.zone, 'Europe/Paris')
|
||||
self.assertEqual(str(now.tzinfo), 'Europe/Paris')
|
||||
|
||||
@requires_tz_support
|
||||
def test_filter_date_field_with_aware_datetime(self):
|
||||
|
@ -871,18 +888,20 @@ class TemplateTests(SimpleTestCase):
|
|||
expected = results[k1][k2]
|
||||
self.assertEqual(actual, expected, '%s / %s: %r != %r' % (k1, k2, actual, expected))
|
||||
|
||||
def test_localtime_filters_with_pytz(self):
|
||||
def test_localtime_filters_with_iana(self):
|
||||
"""
|
||||
Test the |localtime, |utc, and |timezone filters with pytz.
|
||||
Test the |localtime, |utc, and |timezone filters with iana zones.
|
||||
"""
|
||||
# Use a pytz timezone as local time
|
||||
# Use an IANA timezone as local time
|
||||
tpl = Template("{% load tz %}{{ dt|localtime }}|{{ dt|utc }}")
|
||||
ctx = Context({'dt': datetime.datetime(2011, 9, 1, 12, 20, 30)})
|
||||
|
||||
with self.settings(TIME_ZONE='Europe/Paris'):
|
||||
self.assertEqual(tpl.render(ctx), "2011-09-01T12:20:30+02:00|2011-09-01T10:20:30+00:00")
|
||||
|
||||
# Use a pytz timezone as argument
|
||||
# Use an IANA timezone as argument
|
||||
for tz in get_timezones('Europe/Paris'):
|
||||
with self.subTest(repr(tz)):
|
||||
tpl = Template("{% load tz %}{{ dt|timezone:tz }}")
|
||||
ctx = Context({
|
||||
'dt': datetime.datetime(2011, 9, 1, 13, 20, 30),
|
||||
|
@ -890,14 +909,6 @@ class TemplateTests(SimpleTestCase):
|
|||
})
|
||||
self.assertEqual(tpl.render(ctx), "2011-09-01T12:20:30+02:00")
|
||||
|
||||
# Use a pytz timezone name as argument
|
||||
tpl = Template("{% load tz %}{{ dt|timezone:'Europe/Paris' }}")
|
||||
ctx = Context({
|
||||
'dt': datetime.datetime(2011, 9, 1, 13, 20, 30),
|
||||
'tz': pytz.timezone('Europe/Paris'),
|
||||
})
|
||||
self.assertEqual(tpl.render(ctx), "2011-09-01T12:20:30+02:00")
|
||||
|
||||
def test_localtime_templatetag_invalid_argument(self):
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
Template("{% load tz %}{% localtime foo %}{% endlocaltime %}").render()
|
||||
|
@ -945,20 +956,22 @@ class TemplateTests(SimpleTestCase):
|
|||
"2011-09-01T13:20:30+03:00|2011-09-01T17:20:30+07:00|2011-09-01T13:20:30+03:00"
|
||||
)
|
||||
|
||||
def test_timezone_templatetag_with_pytz(self):
|
||||
def test_timezone_templatetag_with_iana(self):
|
||||
"""
|
||||
Test the {% timezone %} templatetag with pytz.
|
||||
Test the {% timezone %} templatetag with IANA time zone providers.
|
||||
"""
|
||||
tpl = Template("{% load tz %}{% timezone tz %}{{ dt }}{% endtimezone %}")
|
||||
|
||||
# Use a pytz timezone as argument
|
||||
# Use a IANA timezone as argument
|
||||
for tz in get_timezones('Europe/Paris'):
|
||||
with self.subTest(repr(tz)):
|
||||
ctx = Context({
|
||||
'dt': datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT),
|
||||
'tz': pytz.timezone('Europe/Paris'),
|
||||
'tz': tz,
|
||||
})
|
||||
self.assertEqual(tpl.render(ctx), "2011-09-01T12:20:30+02:00")
|
||||
|
||||
# Use a pytz timezone name as argument
|
||||
# Use a IANA timezone name as argument
|
||||
ctx = Context({
|
||||
'dt': datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT),
|
||||
'tz': 'Europe/Paris',
|
||||
|
@ -991,12 +1004,14 @@ class TemplateTests(SimpleTestCase):
|
|||
with timezone.override(UTC):
|
||||
self.assertEqual(tpl.render(Context({'tz': ICT})), "+0700")
|
||||
|
||||
def test_get_current_timezone_templatetag_with_pytz(self):
|
||||
def test_get_current_timezone_templatetag_with_iana(self):
|
||||
"""
|
||||
Test the {% get_current_timezone %} templatetag with pytz.
|
||||
"""
|
||||
tpl = Template("{% load tz %}{% get_current_timezone as time_zone %}{{ time_zone }}")
|
||||
with timezone.override(pytz.timezone('Europe/Paris')):
|
||||
for tz in get_timezones('Europe/Paris'):
|
||||
with self.subTest(repr(tz)):
|
||||
with timezone.override(tz):
|
||||
self.assertEqual(tpl.render(Context()), "Europe/Paris")
|
||||
|
||||
tpl = Template(
|
||||
|
@ -1059,14 +1074,18 @@ class LegacyFormsTests(TestCase):
|
|||
|
||||
def test_form_with_non_existent_time(self):
|
||||
form = EventForm({'dt': '2011-03-27 02:30:00'})
|
||||
with timezone.override(pytz.timezone('Europe/Paris')):
|
||||
for tz in get_timezones('Europe/Paris'):
|
||||
with self.subTest(repr(tz)):
|
||||
with timezone.override(tz):
|
||||
# This is a bug.
|
||||
self.assertTrue(form.is_valid())
|
||||
self.assertEqual(form.cleaned_data['dt'], datetime.datetime(2011, 3, 27, 2, 30, 0))
|
||||
|
||||
def test_form_with_ambiguous_time(self):
|
||||
form = EventForm({'dt': '2011-10-30 02:30:00'})
|
||||
with timezone.override(pytz.timezone('Europe/Paris')):
|
||||
for tz in get_timezones('Europe/Paris'):
|
||||
with self.subTest(repr(tz)):
|
||||
with timezone.override(tz):
|
||||
# This is a bug.
|
||||
self.assertTrue(form.is_valid())
|
||||
self.assertEqual(form.cleaned_data['dt'], datetime.datetime(2011, 10, 30, 2, 30, 0))
|
||||
|
@ -1098,7 +1117,9 @@ class NewFormsTests(TestCase):
|
|||
self.assertEqual(form.cleaned_data['dt'], datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC))
|
||||
|
||||
def test_form_with_non_existent_time(self):
|
||||
with timezone.override(pytz.timezone('Europe/Paris')):
|
||||
for tz in get_timezones('Europe/Paris'):
|
||||
with self.subTest(repr(tz)):
|
||||
with timezone.override(tz):
|
||||
form = EventForm({'dt': '2011-03-27 02:30:00'})
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(
|
||||
|
@ -1109,7 +1130,9 @@ class NewFormsTests(TestCase):
|
|||
)
|
||||
|
||||
def test_form_with_ambiguous_time(self):
|
||||
with timezone.override(pytz.timezone('Europe/Paris')):
|
||||
for tz in get_timezones('Europe/Paris'):
|
||||
with self.subTest(repr(tz)):
|
||||
with timezone.override(tz):
|
||||
form = EventForm({'dt': '2011-10-30 02:30:00'})
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(
|
||||
|
|
|
@ -1,14 +1,38 @@
|
|||
import datetime
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
import pytz
|
||||
|
||||
try:
|
||||
import zoneinfo
|
||||
except ImportError:
|
||||
try:
|
||||
from backports import zoneinfo
|
||||
except ImportError:
|
||||
zoneinfo = None
|
||||
|
||||
from django.test import SimpleTestCase, override_settings
|
||||
from django.utils import timezone
|
||||
|
||||
CET = pytz.timezone("Europe/Paris")
|
||||
EAT = timezone.get_fixed_timezone(180) # Africa/Nairobi
|
||||
ICT = timezone.get_fixed_timezone(420) # Asia/Bangkok
|
||||
UTC = datetime.timezone.utc
|
||||
|
||||
HAS_ZONEINFO = zoneinfo is not None
|
||||
|
||||
if not HAS_ZONEINFO:
|
||||
PARIS_ZI = None
|
||||
PARIS_IMPLS = (CET,)
|
||||
|
||||
needs_zoneinfo = unittest.skip("Test requires zoneinfo")
|
||||
else:
|
||||
PARIS_ZI = zoneinfo.ZoneInfo('Europe/Paris')
|
||||
PARIS_IMPLS = (CET, PARIS_ZI)
|
||||
|
||||
def needs_zoneinfo(f):
|
||||
return f
|
||||
|
||||
|
||||
class TimezoneTests(SimpleTestCase):
|
||||
|
@ -142,13 +166,21 @@ class TimezoneTests(SimpleTestCase):
|
|||
)
|
||||
|
||||
def test_make_aware2(self):
|
||||
CEST = datetime.timezone(datetime.timedelta(hours=2), 'CEST')
|
||||
for tz in PARIS_IMPLS:
|
||||
with self.subTest(repr(tz)):
|
||||
self.assertEqual(
|
||||
timezone.make_aware(datetime.datetime(2011, 9, 1, 12, 20, 30), CET),
|
||||
CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)))
|
||||
timezone.make_aware(datetime.datetime(2011, 9, 1, 12, 20, 30), tz),
|
||||
datetime.datetime(2011, 9, 1, 12, 20, 30, tzinfo=CEST))
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
timezone.make_aware(CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)), CET)
|
||||
|
||||
def test_make_aware_pytz(self):
|
||||
if HAS_ZONEINFO:
|
||||
with self.assertRaises(ValueError):
|
||||
timezone.make_aware(datetime.datetime(2011, 9, 1, 12, 20, 30, tzinfo=PARIS_ZI), PARIS_ZI)
|
||||
|
||||
def test_make_naive_pytz(self):
|
||||
self.assertEqual(
|
||||
timezone.make_naive(CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)), CET),
|
||||
datetime.datetime(2011, 9, 1, 12, 20, 30))
|
||||
|
@ -160,6 +192,18 @@ class TimezoneTests(SimpleTestCase):
|
|||
with self.assertRaisesMessage(ValueError, 'make_naive() cannot be applied to a naive datetime'):
|
||||
timezone.make_naive(datetime.datetime(2011, 9, 1, 12, 20, 30), CET)
|
||||
|
||||
@needs_zoneinfo
|
||||
def test_make_naive_zoneinfo(self):
|
||||
self.assertEqual(
|
||||
timezone.make_naive(datetime.datetime(2011, 9, 1, 12, 20, 30, tzinfo=PARIS_ZI), PARIS_ZI),
|
||||
datetime.datetime(2011, 9, 1, 12, 20, 30)
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
timezone.make_naive(datetime.datetime(2011, 9, 1, 12, 20, 30, fold=1, tzinfo=PARIS_ZI), PARIS_ZI),
|
||||
datetime.datetime(2011, 9, 1, 12, 20, 30, fold=1)
|
||||
)
|
||||
|
||||
def test_make_aware_pytz_ambiguous(self):
|
||||
# 2:30 happens twice, once before DST ends and once after
|
||||
ambiguous = datetime.datetime(2015, 10, 25, 2, 30)
|
||||
|
@ -173,6 +217,21 @@ class TimezoneTests(SimpleTestCase):
|
|||
self.assertEqual(std.tzinfo.utcoffset(std), datetime.timedelta(hours=1))
|
||||
self.assertEqual(dst.tzinfo.utcoffset(dst), datetime.timedelta(hours=2))
|
||||
|
||||
@needs_zoneinfo
|
||||
def test_make_aware_zoneinfo_ambiguous(self):
|
||||
# 2:30 happens twice, once before DST ends and once after
|
||||
ambiguous = datetime.datetime(2015, 10, 25, 2, 30)
|
||||
|
||||
std = timezone.make_aware(ambiguous.replace(fold=1), timezone=PARIS_ZI)
|
||||
dst = timezone.make_aware(ambiguous, timezone=PARIS_ZI)
|
||||
|
||||
self.assertEqual(
|
||||
std.astimezone(UTC) - dst.astimezone(UTC),
|
||||
datetime.timedelta(hours=1)
|
||||
)
|
||||
self.assertEqual(std.utcoffset(), datetime.timedelta(hours=1))
|
||||
self.assertEqual(dst.utcoffset(), datetime.timedelta(hours=2))
|
||||
|
||||
def test_make_aware_pytz_non_existent(self):
|
||||
# 2:30 never happened due to DST
|
||||
non_existent = datetime.datetime(2015, 3, 29, 2, 30)
|
||||
|
@ -186,6 +245,21 @@ class TimezoneTests(SimpleTestCase):
|
|||
self.assertEqual(std.tzinfo.utcoffset(std), datetime.timedelta(hours=1))
|
||||
self.assertEqual(dst.tzinfo.utcoffset(dst), datetime.timedelta(hours=2))
|
||||
|
||||
@needs_zoneinfo
|
||||
def test_make_aware_zoneinfo_non_existent(self):
|
||||
# 2:30 never happened due to DST
|
||||
non_existent = datetime.datetime(2015, 3, 29, 2, 30)
|
||||
|
||||
std = timezone.make_aware(non_existent, PARIS_ZI)
|
||||
dst = timezone.make_aware(non_existent.replace(fold=1), PARIS_ZI)
|
||||
|
||||
self.assertEqual(
|
||||
std.astimezone(UTC) - dst.astimezone(UTC),
|
||||
datetime.timedelta(hours=1)
|
||||
)
|
||||
self.assertEqual(std.utcoffset(), datetime.timedelta(hours=1))
|
||||
self.assertEqual(dst.utcoffset(), datetime.timedelta(hours=2))
|
||||
|
||||
def test_get_default_timezone(self):
|
||||
self.assertEqual(timezone.get_default_timezone_name(), 'America/Chicago')
|
||||
|
||||
|
|
Loading…
Reference in New Issue