2012-04-29 13:37:23 +00:00
|
|
|
import datetime
|
2021-01-19 10:16:01 +00:00
|
|
|
import unittest
|
2017-01-19 17:16:04 +00:00
|
|
|
from unittest import mock
|
2016-10-08 01:06:49 +00:00
|
|
|
|
2021-09-09 13:15:44 +00:00
|
|
|
try:
|
|
|
|
import pytz
|
|
|
|
except ImportError:
|
|
|
|
pytz = None
|
2013-07-01 12:22:27 +00:00
|
|
|
|
2021-01-19 10:16:01 +00:00
|
|
|
try:
|
|
|
|
import zoneinfo
|
|
|
|
except ImportError:
|
2021-09-09 13:15:44 +00:00
|
|
|
from backports import zoneinfo
|
2021-01-19 10:16:01 +00:00
|
|
|
|
2021-09-09 13:15:44 +00:00
|
|
|
from django.test import SimpleTestCase, ignore_warnings, override_settings
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.utils import timezone
|
2021-09-09 13:15:44 +00:00
|
|
|
from django.utils.deprecation import RemovedInDjango50Warning
|
2015-01-28 12:35:27 +00:00
|
|
|
|
2021-09-09 13:15:44 +00:00
|
|
|
PARIS_ZI = zoneinfo.ZoneInfo('Europe/Paris')
|
2013-09-08 07:04:31 +00:00
|
|
|
EAT = timezone.get_fixed_timezone(180) # Africa/Nairobi
|
|
|
|
ICT = timezone.get_fixed_timezone(420) # Asia/Bangkok
|
2021-01-19 10:16:01 +00:00
|
|
|
UTC = datetime.timezone.utc
|
|
|
|
|
2021-09-09 13:15:44 +00:00
|
|
|
HAS_PYTZ = pytz is not None
|
|
|
|
if not HAS_PYTZ:
|
|
|
|
CET = None
|
|
|
|
PARIS_IMPLS = (PARIS_ZI,)
|
2021-01-19 10:16:01 +00:00
|
|
|
|
2021-09-09 13:15:44 +00:00
|
|
|
needs_pytz = unittest.skip('Test requires pytz')
|
2021-01-19 10:16:01 +00:00
|
|
|
else:
|
2021-09-09 13:15:44 +00:00
|
|
|
CET = pytz.timezone('Europe/Paris')
|
|
|
|
PARIS_IMPLS = (PARIS_ZI, CET)
|
2021-01-19 10:16:01 +00:00
|
|
|
|
2021-09-09 13:15:44 +00:00
|
|
|
def needs_pytz(f):
|
2021-01-19 10:16:01 +00:00
|
|
|
return f
|
2013-01-31 15:00:39 +00:00
|
|
|
|
2016-08-09 22:14:15 +00:00
|
|
|
|
|
|
|
class TimezoneTests(SimpleTestCase):
|
2011-11-18 13:01:06 +00:00
|
|
|
|
2021-09-09 13:15:44 +00:00
|
|
|
def setUp(self):
|
|
|
|
# RemovedInDjango50Warning
|
|
|
|
timezone.get_default_timezone.cache_clear()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# RemovedInDjango50Warning
|
|
|
|
timezone.get_default_timezone.cache_clear()
|
|
|
|
|
|
|
|
def test_default_timezone_is_zoneinfo(self):
|
|
|
|
self.assertIsInstance(timezone.get_default_timezone(), zoneinfo.ZoneInfo)
|
|
|
|
|
|
|
|
@needs_pytz
|
|
|
|
@ignore_warnings(category=RemovedInDjango50Warning)
|
|
|
|
@override_settings(USE_DEPRECATED_PYTZ=True)
|
|
|
|
def test_setting_allows_fallback_to_pytz(self):
|
|
|
|
self.assertIsInstance(timezone.get_default_timezone(), pytz.BaseTzInfo)
|
|
|
|
|
2012-04-29 13:37:23 +00:00
|
|
|
def test_now(self):
|
|
|
|
with override_settings(USE_TZ=True):
|
|
|
|
self.assertTrue(timezone.is_aware(timezone.now()))
|
|
|
|
with override_settings(USE_TZ=False):
|
|
|
|
self.assertTrue(timezone.is_naive(timezone.now()))
|
|
|
|
|
2016-09-01 00:19:33 +00:00
|
|
|
def test_localdate(self):
|
|
|
|
naive = datetime.datetime(2015, 1, 1, 0, 0, 1)
|
2016-11-05 11:33:22 +00:00
|
|
|
with self.assertRaisesMessage(ValueError, 'localtime() cannot be applied to a naive datetime'):
|
|
|
|
timezone.localdate(naive)
|
|
|
|
with self.assertRaisesMessage(ValueError, 'localtime() cannot be applied to a naive datetime'):
|
|
|
|
timezone.localdate(naive, timezone=EAT)
|
2016-09-01 00:19:33 +00:00
|
|
|
|
|
|
|
aware = datetime.datetime(2015, 1, 1, 0, 0, 1, tzinfo=ICT)
|
|
|
|
self.assertEqual(timezone.localdate(aware, timezone=EAT), datetime.date(2014, 12, 31))
|
|
|
|
with timezone.override(EAT):
|
|
|
|
self.assertEqual(timezone.localdate(aware), datetime.date(2014, 12, 31))
|
|
|
|
|
|
|
|
with mock.patch('django.utils.timezone.now', return_value=aware):
|
|
|
|
self.assertEqual(timezone.localdate(timezone=EAT), datetime.date(2014, 12, 31))
|
|
|
|
with timezone.override(EAT):
|
|
|
|
self.assertEqual(timezone.localdate(), datetime.date(2014, 12, 31))
|
|
|
|
|
2013-01-31 15:00:39 +00:00
|
|
|
def test_override(self):
|
|
|
|
default = timezone.get_default_timezone()
|
|
|
|
try:
|
|
|
|
timezone.activate(ICT)
|
|
|
|
|
|
|
|
with timezone.override(EAT):
|
|
|
|
self.assertIs(EAT, timezone.get_current_timezone())
|
|
|
|
self.assertIs(ICT, timezone.get_current_timezone())
|
|
|
|
|
|
|
|
with timezone.override(None):
|
|
|
|
self.assertIs(default, timezone.get_current_timezone())
|
|
|
|
self.assertIs(ICT, timezone.get_current_timezone())
|
|
|
|
|
|
|
|
timezone.deactivate()
|
|
|
|
|
|
|
|
with timezone.override(EAT):
|
|
|
|
self.assertIs(EAT, timezone.get_current_timezone())
|
|
|
|
self.assertIs(default, timezone.get_current_timezone())
|
|
|
|
|
|
|
|
with timezone.override(None):
|
|
|
|
self.assertIs(default, timezone.get_current_timezone())
|
|
|
|
self.assertIs(default, timezone.get_current_timezone())
|
|
|
|
finally:
|
|
|
|
timezone.deactivate()
|
|
|
|
|
2014-08-30 18:06:38 +00:00
|
|
|
def test_override_decorator(self):
|
|
|
|
default = timezone.get_default_timezone()
|
|
|
|
|
|
|
|
@timezone.override(EAT)
|
|
|
|
def func_tz_eat():
|
|
|
|
self.assertIs(EAT, timezone.get_current_timezone())
|
|
|
|
|
|
|
|
@timezone.override(None)
|
|
|
|
def func_tz_none():
|
|
|
|
self.assertIs(default, timezone.get_current_timezone())
|
|
|
|
|
|
|
|
try:
|
|
|
|
timezone.activate(ICT)
|
|
|
|
|
|
|
|
func_tz_eat()
|
|
|
|
self.assertIs(ICT, timezone.get_current_timezone())
|
|
|
|
|
|
|
|
func_tz_none()
|
|
|
|
self.assertIs(ICT, timezone.get_current_timezone())
|
|
|
|
|
|
|
|
timezone.deactivate()
|
|
|
|
|
|
|
|
func_tz_eat()
|
|
|
|
self.assertIs(default, timezone.get_current_timezone())
|
|
|
|
|
|
|
|
func_tz_none()
|
|
|
|
self.assertIs(default, timezone.get_current_timezone())
|
|
|
|
finally:
|
|
|
|
timezone.deactivate()
|
|
|
|
|
2016-11-01 18:01:40 +00:00
|
|
|
def test_override_string_tz(self):
|
|
|
|
with timezone.override('Asia/Bangkok'):
|
|
|
|
self.assertEqual(timezone.get_current_timezone_name(), 'Asia/Bangkok')
|
|
|
|
|
|
|
|
def test_override_fixed_offset(self):
|
2018-07-09 20:33:36 +00:00
|
|
|
with timezone.override(datetime.timezone(datetime.timedelta(), 'tzname')):
|
2016-11-01 18:01:40 +00:00
|
|
|
self.assertEqual(timezone.get_current_timezone_name(), 'tzname')
|
|
|
|
|
|
|
|
def test_activate_invalid_timezone(self):
|
|
|
|
with self.assertRaisesMessage(ValueError, 'Invalid timezone: None'):
|
|
|
|
timezone.activate(None)
|
|
|
|
|
2014-05-16 20:52:10 +00:00
|
|
|
def test_is_aware(self):
|
|
|
|
self.assertTrue(timezone.is_aware(datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)))
|
|
|
|
self.assertFalse(timezone.is_aware(datetime.datetime(2011, 9, 1, 13, 20, 30)))
|
|
|
|
|
|
|
|
def test_is_naive(self):
|
|
|
|
self.assertFalse(timezone.is_naive(datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)))
|
|
|
|
self.assertTrue(timezone.is_naive(datetime.datetime(2011, 9, 1, 13, 20, 30)))
|
|
|
|
|
|
|
|
def test_make_aware(self):
|
|
|
|
self.assertEqual(
|
|
|
|
timezone.make_aware(datetime.datetime(2011, 9, 1, 13, 20, 30), EAT),
|
|
|
|
datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT))
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
timezone.make_aware(datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT), EAT)
|
|
|
|
|
|
|
|
def test_make_naive(self):
|
|
|
|
self.assertEqual(
|
|
|
|
timezone.make_naive(datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT), EAT),
|
|
|
|
datetime.datetime(2011, 9, 1, 13, 20, 30))
|
|
|
|
self.assertEqual(
|
|
|
|
timezone.make_naive(datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=ICT), EAT),
|
|
|
|
datetime.datetime(2011, 9, 1, 13, 20, 30))
|
2016-08-09 22:14:15 +00:00
|
|
|
|
2016-11-05 11:33:22 +00:00
|
|
|
with self.assertRaisesMessage(ValueError, 'make_naive() cannot be applied to a naive datetime'):
|
|
|
|
timezone.make_naive(datetime.datetime(2011, 9, 1, 13, 20, 30), EAT)
|
2014-05-16 21:12:59 +00:00
|
|
|
|
2016-11-01 18:01:40 +00:00
|
|
|
def test_make_naive_no_tz(self):
|
|
|
|
self.assertEqual(
|
|
|
|
timezone.make_naive(datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)),
|
|
|
|
datetime.datetime(2011, 9, 1, 5, 20, 30)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_make_aware_no_tz(self):
|
|
|
|
self.assertEqual(
|
|
|
|
timezone.make_aware(datetime.datetime(2011, 9, 1, 13, 20, 30)),
|
|
|
|
datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=timezone.get_fixed_timezone(-300))
|
|
|
|
)
|
|
|
|
|
2014-05-17 16:54:34 +00:00
|
|
|
def test_make_aware2(self):
|
2021-01-19 10:16:01 +00:00
|
|
|
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), tz),
|
|
|
|
datetime.datetime(2011, 9, 1, 12, 20, 30, tzinfo=CEST))
|
|
|
|
|
2021-09-09 13:15:44 +00:00
|
|
|
if HAS_PYTZ:
|
2021-01-19 10:16:01 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2021-09-09 13:15:44 +00:00
|
|
|
timezone.make_aware(CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)), CET)
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
timezone.make_aware(datetime.datetime(2011, 9, 1, 12, 20, 30, tzinfo=PARIS_ZI), PARIS_ZI)
|
2021-01-19 10:16:01 +00:00
|
|
|
|
2021-09-09 13:15:44 +00:00
|
|
|
@needs_pytz
|
2021-01-19 10:16:01 +00:00
|
|
|
def test_make_naive_pytz(self):
|
2014-05-16 21:12:59 +00:00
|
|
|
self.assertEqual(
|
|
|
|
timezone.make_naive(CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)), CET),
|
|
|
|
datetime.datetime(2011, 9, 1, 12, 20, 30))
|
|
|
|
self.assertEqual(
|
2015-09-11 23:33:12 +00:00
|
|
|
timezone.make_naive(
|
|
|
|
pytz.timezone("Asia/Bangkok").localize(datetime.datetime(2011, 9, 1, 17, 20, 30)), CET
|
|
|
|
),
|
2014-05-16 21:12:59 +00:00
|
|
|
datetime.datetime(2011, 9, 1, 12, 20, 30))
|
2016-11-05 11:33:22 +00:00
|
|
|
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)
|
2015-03-31 05:58:37 +00:00
|
|
|
|
2021-01-19 10:16:01 +00:00
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
2021-09-09 13:15:44 +00:00
|
|
|
@needs_pytz
|
|
|
|
@ignore_warnings(category=RemovedInDjango50Warning)
|
2015-03-31 05:58:37 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
with self.assertRaises(pytz.AmbiguousTimeError):
|
|
|
|
timezone.make_aware(ambiguous, timezone=CET)
|
|
|
|
|
|
|
|
std = timezone.make_aware(ambiguous, timezone=CET, is_dst=False)
|
|
|
|
dst = timezone.make_aware(ambiguous, timezone=CET, is_dst=True)
|
|
|
|
self.assertEqual(std - dst, datetime.timedelta(hours=1))
|
|
|
|
self.assertEqual(std.tzinfo.utcoffset(std), datetime.timedelta(hours=1))
|
|
|
|
self.assertEqual(dst.tzinfo.utcoffset(dst), datetime.timedelta(hours=2))
|
|
|
|
|
2021-01-19 10:16:01 +00:00
|
|
|
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))
|
|
|
|
|
2021-09-09 13:15:44 +00:00
|
|
|
@needs_pytz
|
|
|
|
@ignore_warnings(category=RemovedInDjango50Warning)
|
2015-03-31 05:58:37 +00:00
|
|
|
def test_make_aware_pytz_non_existent(self):
|
|
|
|
# 2:30 never happened due to DST
|
|
|
|
non_existent = datetime.datetime(2015, 3, 29, 2, 30)
|
|
|
|
|
|
|
|
with self.assertRaises(pytz.NonExistentTimeError):
|
|
|
|
timezone.make_aware(non_existent, timezone=CET)
|
|
|
|
|
|
|
|
std = timezone.make_aware(non_existent, timezone=CET, is_dst=False)
|
|
|
|
dst = timezone.make_aware(non_existent, timezone=CET, is_dst=True)
|
|
|
|
self.assertEqual(std - dst, datetime.timedelta(hours=1))
|
|
|
|
self.assertEqual(std.tzinfo.utcoffset(std), datetime.timedelta(hours=1))
|
|
|
|
self.assertEqual(dst.tzinfo.utcoffset(dst), datetime.timedelta(hours=2))
|
2016-11-01 18:01:40 +00:00
|
|
|
|
2021-01-19 10:16:01 +00:00
|
|
|
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))
|
|
|
|
|
2021-09-09 13:15:44 +00:00
|
|
|
def test_make_aware_is_dst_deprecation_warning(self):
|
|
|
|
msg = (
|
|
|
|
'The is_dst argument to make_aware(), used by the Trunc() '
|
|
|
|
'database functions and QuerySet.datetimes(), is deprecated as it '
|
|
|
|
'has no effect with zoneinfo time zones.'
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(RemovedInDjango50Warning, msg):
|
|
|
|
timezone.make_aware(datetime.datetime(2011, 9, 1, 13, 20, 30), EAT, is_dst=True)
|
|
|
|
|
2021-08-24 09:21:32 +00:00
|
|
|
def test_get_timezone_name(self):
|
|
|
|
"""
|
|
|
|
The _get_timezone_name() helper must return the offset for fixed offset
|
|
|
|
timezones, for usage with Trunc DB functions.
|
|
|
|
|
|
|
|
The datetime.timezone examples show the current behavior.
|
|
|
|
"""
|
|
|
|
tests = [
|
|
|
|
# datetime.timezone, fixed offset with and without `name`.
|
|
|
|
(datetime.timezone(datetime.timedelta(hours=10)), 'UTC+10:00'),
|
|
|
|
(datetime.timezone(datetime.timedelta(hours=10), name='Etc/GMT-10'), 'Etc/GMT-10'),
|
2021-09-09 13:15:44 +00:00
|
|
|
# zoneinfo, named and fixed offset.
|
|
|
|
(zoneinfo.ZoneInfo('Europe/Madrid'), 'Europe/Madrid'),
|
|
|
|
(zoneinfo.ZoneInfo('Etc/GMT-10'), '+10'),
|
2021-08-24 09:21:32 +00:00
|
|
|
]
|
2021-09-09 13:15:44 +00:00
|
|
|
if HAS_PYTZ:
|
2021-08-24 09:21:32 +00:00
|
|
|
tests += [
|
2021-09-09 13:15:44 +00:00
|
|
|
# pytz, named and fixed offset.
|
|
|
|
(pytz.timezone('Europe/Madrid'), 'Europe/Madrid'),
|
|
|
|
(pytz.timezone('Etc/GMT-10'), '+10'),
|
2021-08-24 09:21:32 +00:00
|
|
|
]
|
|
|
|
for tz, expected in tests:
|
|
|
|
with self.subTest(tz=tz, expected=expected):
|
|
|
|
self.assertEqual(timezone._get_timezone_name(tz), expected)
|
|
|
|
|
2016-11-01 18:01:40 +00:00
|
|
|
def test_get_default_timezone(self):
|
|
|
|
self.assertEqual(timezone.get_default_timezone_name(), 'America/Chicago')
|
|
|
|
|
|
|
|
def test_fixedoffset_timedelta(self):
|
|
|
|
delta = datetime.timedelta(hours=1)
|
2018-07-09 20:33:36 +00:00
|
|
|
self.assertEqual(timezone.get_fixed_timezone(delta).utcoffset(None), delta)
|
2016-11-01 18:01:40 +00:00
|
|
|
|
2017-10-24 19:42:44 +00:00
|
|
|
def test_fixedoffset_negative_timedelta(self):
|
|
|
|
delta = datetime.timedelta(hours=-2)
|
2018-07-09 20:33:36 +00:00
|
|
|
self.assertEqual(timezone.get_fixed_timezone(delta).utcoffset(None), delta)
|