2011-11-18 21:01:06 +08:00
|
|
|
import copy
|
2012-04-29 21:37:23 +08:00
|
|
|
import datetime
|
2011-11-18 21:01:06 +08:00
|
|
|
import pickle
|
2016-08-10 06:14:15 +08:00
|
|
|
import sys
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
|
|
|
|
2016-08-10 06:14:15 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils import timezone
|
|
|
|
|
2014-05-17 05:12:59 +08:00
|
|
|
try:
|
|
|
|
import pytz
|
|
|
|
except ImportError:
|
|
|
|
pytz = None
|
|
|
|
|
2015-05-03 15:08:37 +08:00
|
|
|
requires_pytz = unittest.skipIf(pytz is None, "this test requires pytz")
|
|
|
|
|
2014-05-17 05:12:59 +08:00
|
|
|
if pytz is not None:
|
|
|
|
CET = pytz.timezone("Europe/Paris")
|
2013-09-08 15:04:31 +08:00
|
|
|
EAT = timezone.get_fixed_timezone(180) # Africa/Nairobi
|
|
|
|
ICT = timezone.get_fixed_timezone(420) # Asia/Bangkok
|
2013-01-31 23:00:39 +08:00
|
|
|
|
2016-08-10 06:14:15 +08:00
|
|
|
PY36 = sys.version_info >= (3, 6)
|
2013-01-31 23:00:39 +08:00
|
|
|
|
2016-08-10 06:14:15 +08:00
|
|
|
|
|
|
|
class TimezoneTests(SimpleTestCase):
|
2011-11-18 21:01:06 +08:00
|
|
|
|
2012-04-29 21:37:23 +08:00
|
|
|
def test_localtime(self):
|
|
|
|
now = datetime.datetime.utcnow().replace(tzinfo=timezone.utc)
|
|
|
|
local_tz = timezone.LocalTimezone()
|
|
|
|
local_now = timezone.localtime(now, local_tz)
|
|
|
|
self.assertEqual(local_now.tzinfo, local_tz)
|
|
|
|
|
2014-05-17 05:12:59 +08:00
|
|
|
def test_localtime_naive(self):
|
2016-08-10 06:14:15 +08:00
|
|
|
now = datetime.datetime.now()
|
|
|
|
if PY36:
|
|
|
|
self.assertEqual(timezone.localtime(now), now.replace(tzinfo=timezone.LocalTimezone()))
|
|
|
|
else:
|
|
|
|
with self.assertRaisesMessage(ValueError, 'astimezone() cannot be applied to a naive datetime'):
|
|
|
|
timezone.localtime(now)
|
2014-05-17 05:12:59 +08:00
|
|
|
|
2013-09-08 07:56:49 +08:00
|
|
|
def test_localtime_out_of_range(self):
|
|
|
|
local_tz = timezone.LocalTimezone()
|
|
|
|
long_ago = datetime.datetime(1900, 1, 1, tzinfo=timezone.utc)
|
2013-09-09 01:38:12 +08:00
|
|
|
try:
|
2013-09-08 07:56:49 +08:00
|
|
|
timezone.localtime(long_ago, local_tz)
|
2013-09-09 02:43:04 +08:00
|
|
|
except (OverflowError, ValueError) as exc:
|
2013-09-09 01:38:12 +08:00
|
|
|
self.assertIn("install pytz", exc.args[0])
|
|
|
|
else:
|
2016-08-17 04:42:27 +08:00
|
|
|
self.skipTest("Failed to trigger an OverflowError or ValueError")
|
2013-09-08 07:56:49 +08:00
|
|
|
|
2012-04-29 21:37:23 +08: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()))
|
|
|
|
|
2013-01-31 23:00:39 +08: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-31 02:06:38 +08: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()
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
def test_copy(self):
|
2012-04-29 21:37:23 +08:00
|
|
|
self.assertIsInstance(copy.copy(timezone.UTC()), timezone.UTC)
|
|
|
|
self.assertIsInstance(copy.copy(timezone.LocalTimezone()), timezone.LocalTimezone)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
def test_deepcopy(self):
|
2012-04-29 21:37:23 +08:00
|
|
|
self.assertIsInstance(copy.deepcopy(timezone.UTC()), timezone.UTC)
|
|
|
|
self.assertIsInstance(copy.deepcopy(timezone.LocalTimezone()), timezone.LocalTimezone)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
def test_pickling_unpickling(self):
|
2012-04-29 21:37:23 +08:00
|
|
|
self.assertIsInstance(pickle.loads(pickle.dumps(timezone.UTC())), timezone.UTC)
|
|
|
|
self.assertIsInstance(pickle.loads(pickle.dumps(timezone.LocalTimezone())), timezone.LocalTimezone)
|
2014-05-17 04:52:10 +08: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-10 06:14:15 +08:00
|
|
|
|
|
|
|
args = (datetime.datetime(2011, 9, 1, 13, 20, 30), EAT)
|
|
|
|
if PY36:
|
|
|
|
self.assertEqual(timezone.make_naive(*args), datetime.datetime(2011, 9, 1, 21, 20, 30))
|
|
|
|
else:
|
|
|
|
with self.assertRaisesMessage(ValueError, 'astimezone() cannot be applied to a naive datetime'):
|
|
|
|
timezone.make_naive(*args)
|
2014-05-17 05:12:59 +08:00
|
|
|
|
2015-05-03 15:08:37 +08:00
|
|
|
@requires_pytz
|
2014-05-18 00:54:34 +08:00
|
|
|
def test_make_aware2(self):
|
2014-05-17 05:12:59 +08:00
|
|
|
self.assertEqual(
|
|
|
|
timezone.make_aware(datetime.datetime(2011, 9, 1, 12, 20, 30), CET),
|
|
|
|
CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)))
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
timezone.make_aware(CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)), CET)
|
|
|
|
|
2015-05-03 15:08:37 +08:00
|
|
|
@requires_pytz
|
2014-05-17 05:12:59 +08:00
|
|
|
def test_make_aware_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))
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
timezone.make_naive(
|
|
|
|
pytz.timezone("Asia/Bangkok").localize(datetime.datetime(2011, 9, 1, 17, 20, 30)), CET
|
|
|
|
),
|
2014-05-17 05:12:59 +08:00
|
|
|
datetime.datetime(2011, 9, 1, 12, 20, 30))
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
timezone.make_naive(datetime.datetime(2011, 9, 1, 12, 20, 30), CET)
|
2015-03-31 13:58:37 +08:00
|
|
|
|
2015-05-03 15:08:37 +08:00
|
|
|
@requires_pytz
|
2015-03-31 13:58:37 +08: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))
|
|
|
|
|
2015-05-03 15:08:37 +08:00
|
|
|
@requires_pytz
|
2015-03-31 13:58:37 +08: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))
|
|
|
|
|
|
|
|
# round trip to UTC then back to CET
|
|
|
|
std = timezone.localtime(timezone.localtime(std, timezone.UTC()), CET)
|
|
|
|
dst = timezone.localtime(timezone.localtime(dst, timezone.UTC()), CET)
|
|
|
|
self.assertEqual((std.hour, std.minute), (3, 30))
|
|
|
|
self.assertEqual((dst.hour, dst.minute), (1, 30))
|