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
|
2012-04-29 21:37:23 +08:00
|
|
|
from django.test.utils import override_settings
|
2013-09-08 07:56:49 +08:00
|
|
|
from django.utils import six
|
2012-04-29 21:37:23 +08:00
|
|
|
from django.utils import timezone
|
2013-01-31 23:00:39 +08:00
|
|
|
from django.utils.tzinfo import FixedOffset
|
2011-11-18 21:01:06 +08:00
|
|
|
from django.utils import unittest
|
|
|
|
|
2012-04-29 21:37:23 +08:00
|
|
|
|
2013-01-31 23:00:39 +08:00
|
|
|
EAT = FixedOffset(180) # Africa/Nairobi
|
|
|
|
ICT = FixedOffset(420) # Asia/Bangkok
|
|
|
|
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
class TimezoneTests(unittest.TestCase):
|
|
|
|
|
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)
|
|
|
|
|
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:
|
2013-09-09 02:43:04 +08:00
|
|
|
raise unittest.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()
|
|
|
|
|
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)
|