2011-11-18 21:01:06 +08:00
|
|
|
import copy
|
2011-10-14 05:19:48 +08:00
|
|
|
import datetime
|
|
|
|
import os
|
2011-11-18 21:01:06 +08:00
|
|
|
import pickle
|
2011-10-14 05:19:48 +08:00
|
|
|
import time
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2013-09-08 15:04:31 +08:00
|
|
|
import warnings
|
2013-07-01 20:22:27 +08:00
|
|
|
|
2013-09-08 15:04:31 +08:00
|
|
|
from django.test.utils import IgnorePendingDeprecationWarningsMixin
|
2014-02-27 05:48:20 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango19Warning
|
|
|
|
|
2010-01-11 01:28:20 +08:00
|
|
|
|
2013-09-08 15:04:31 +08:00
|
|
|
# Swallow the import-time warning to test the deprecated implementation.
|
|
|
|
with warnings.catch_warnings():
|
2014-02-27 05:48:20 +08:00
|
|
|
warnings.filterwarnings("ignore", category=RemovedInDjango19Warning)
|
2013-09-08 15:04:31 +08:00
|
|
|
from django.utils.tzinfo import FixedOffset, LocalTimezone
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-09-08 15:04:31 +08:00
|
|
|
class TzinfoTests(IgnorePendingDeprecationWarningsMixin, unittest.TestCase):
|
2010-09-27 23:14:06 +08:00
|
|
|
|
2011-10-14 05:19:48 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.old_TZ = os.environ.get('TZ')
|
|
|
|
os.environ['TZ'] = 'US/Eastern'
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Check if a timezone has been set
|
|
|
|
time.tzset()
|
|
|
|
cls.tz_tests = True
|
|
|
|
except AttributeError:
|
|
|
|
# No timezone available. Don't run the tests that require a TZ
|
|
|
|
cls.tz_tests = False
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
if cls.old_TZ is None:
|
|
|
|
del os.environ['TZ']
|
|
|
|
else:
|
|
|
|
os.environ['TZ'] = cls.old_TZ
|
|
|
|
|
|
|
|
# Cleanup - force re-evaluation of TZ environment variable.
|
|
|
|
if cls.tz_tests:
|
|
|
|
time.tzset()
|
|
|
|
|
2010-09-27 23:14:06 +08:00
|
|
|
def test_fixedoffset(self):
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(repr(FixedOffset(0)), '+0000')
|
|
|
|
self.assertEqual(repr(FixedOffset(60)), '+0100')
|
|
|
|
self.assertEqual(repr(FixedOffset(-60)), '-0100')
|
|
|
|
self.assertEqual(repr(FixedOffset(280)), '+0440')
|
|
|
|
self.assertEqual(repr(FixedOffset(-280)), '-0440')
|
|
|
|
self.assertEqual(repr(FixedOffset(-78.4)), '-0118')
|
|
|
|
self.assertEqual(repr(FixedOffset(78.4)), '+0118')
|
2013-11-03 05:34:05 +08:00
|
|
|
self.assertEqual(repr(FixedOffset(-5.5 * 60)), '-0530')
|
|
|
|
self.assertEqual(repr(FixedOffset(5.5 * 60)), '+0530')
|
|
|
|
self.assertEqual(repr(FixedOffset(-.5 * 60)), '-0030')
|
|
|
|
self.assertEqual(repr(FixedOffset(.5 * 60)), '+0030')
|
2011-10-14 05:19:48 +08:00
|
|
|
|
|
|
|
def test_16899(self):
|
|
|
|
if not self.tz_tests:
|
|
|
|
return
|
|
|
|
ts = 1289106000
|
|
|
|
# Midnight at the end of DST in US/Eastern: 2010-11-07T05:00:00Z
|
|
|
|
dt = datetime.datetime.utcfromtimestamp(ts)
|
|
|
|
# US/Eastern -- we force its representation to "EST"
|
|
|
|
tz = LocalTimezone(dt + datetime.timedelta(days=1))
|
|
|
|
self.assertEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
repr(datetime.datetime.fromtimestamp(ts - 3600, tz)),
|
|
|
|
'datetime.datetime(2010, 11, 7, 0, 0, tzinfo=EST)')
|
2011-10-14 05:19:48 +08:00
|
|
|
self.assertEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
repr(datetime.datetime.fromtimestamp(ts, tz)),
|
|
|
|
'datetime.datetime(2010, 11, 7, 1, 0, tzinfo=EST)')
|
2011-10-14 05:19:48 +08:00
|
|
|
self.assertEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
repr(datetime.datetime.fromtimestamp(ts + 3600, tz)),
|
|
|
|
'datetime.datetime(2010, 11, 7, 1, 0, tzinfo=EST)')
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
def test_copy(self):
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
self.assertIsInstance(copy.copy(FixedOffset(90)), FixedOffset)
|
|
|
|
self.assertIsInstance(copy.copy(LocalTimezone(now)), LocalTimezone)
|
|
|
|
|
|
|
|
def test_deepcopy(self):
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
self.assertIsInstance(copy.deepcopy(FixedOffset(90)), FixedOffset)
|
|
|
|
self.assertIsInstance(copy.deepcopy(LocalTimezone(now)), LocalTimezone)
|
|
|
|
|
|
|
|
def test_pickling_unpickling(self):
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
self.assertIsInstance(pickle.loads(pickle.dumps(FixedOffset(90))), FixedOffset)
|
|
|
|
self.assertIsInstance(pickle.loads(pickle.dumps(LocalTimezone(now))), LocalTimezone)
|