diff options
author | Stuart Bishop <stuart@stuartbishop.net> | 2012-04-16 14:12:31 +1000 |
---|---|---|
committer | Stuart Bishop <stuart@stuartbishop.net> | 2012-04-16 14:12:31 +1000 |
commit | 8620d04bf9fe93233abe4ade0441cac19ae76342 (patch) | |
tree | 2b41fe6464323be557ca5197225778e5948cbfae /src | |
parent | 886c0b5f08def2201d2fd8a2446f469e65e4c8f5 (diff) | |
download | pytz-8620d04bf9fe93233abe4ade0441cac19ae76342.tar.gz |
Tests for Bug #965481
Diffstat (limited to 'src')
-rw-r--r-- | src/pytz/__init__.py | 10 | ||||
-rw-r--r-- | src/pytz/tests/test_tzinfo.py | 36 |
2 files changed, 38 insertions, 8 deletions
diff --git a/src/pytz/__init__.py b/src/pytz/__init__.py index f3437ef..921eddf 100644 --- a/src/pytz/__init__.py +++ b/src/pytz/__init__.py @@ -199,12 +199,8 @@ HOUR = datetime.timedelta(hours=1) class UTC(datetime.tzinfo): """UTC - Identical to the reference UTC implementation given in Python docs except - that it unpickles using the single module global instance defined beneath - this class declaration. - - Also contains extra attributes and methods to match other pytz tzinfo - instances. + Optimized UTC implementation. It unpickles using the single module global + instance defined beneath this class declaration. """ zone = "UTC" @@ -237,6 +233,8 @@ class UTC(datetime.tzinfo): def normalize(self, dt, is_dst=False): '''Correct the timezone information on the given datetime''' + if dt.tzinfo is self: + return dt if dt.tzinfo is None: raise ValueError('Naive time - no tzinfo set') return dt.replace(tzinfo=self) diff --git a/src/pytz/tests/test_tzinfo.py b/src/pytz/tests/test_tzinfo.py index 3c5d62b..92a8d57 100644 --- a/src/pytz/tests/test_tzinfo.py +++ b/src/pytz/tests/test_tzinfo.py @@ -725,7 +725,7 @@ class BaseTzInfoTestCase: These tests are run for each type of tzinfo. ''' tz = None # override - tzclass = None # override + tz_class = None # override def test_expectedclass(self): self.assertTrue(isinstance(self.tz, self.tz_class)) @@ -750,12 +750,44 @@ class BaseTzInfoTestCase: dt3 = new_tz.localize(dt1) self.assertRaises(ValueError, self.tz.fromutc, dt3) + def test_normalize(self): + other_tz = pytz.timezone('Europe/Paris') + self.assertTrue(self.tz is not other_tz) -class UTCTestCase(unittest.TestCase, BaseTzInfoTestCase): + dt = datetime(2012, 3, 26, 12, 0) + other_dt = other_tz.localize(dt) + + local_dt = self.tz.normalize(other_dt) + + self.assertTrue(local_dt.tzinfo is not other_dt.tzinfo) + self.assertNotEqual( + local_dt.replace(tzinfo=None), other_dt.replace(tzinfo=None)) + + def test_astimezone(self): + other_tz = pytz.timezone('Europe/Paris') + self.assertTrue(self.tz is not other_tz) + + dt = datetime(2012, 3, 26, 12, 0) + other_dt = other_tz.localize(dt) + + local_dt = other_dt.astimezone(self.tz) + + self.assertTrue(local_dt.tzinfo is not other_dt.tzinfo) + self.assertNotEqual( + local_dt.replace(tzinfo=None), other_dt.replace(tzinfo=None)) + + +class OptimizedUTCTestCase(unittest.TestCase, BaseTzInfoTestCase): tz = pytz.utc tz_class = tz.__class__ +class LegacyUTCTestCase(unittest.TestCase, BaseTzInfoTestCase): + # Deprecated timezone, but useful for comparison tests. + tz = pytz.timezone('Etc/UTC') + tz_class = StaticTzInfo + + class StaticTzInfoTestCase(unittest.TestCase, BaseTzInfoTestCase): tz = pytz.timezone('GMT') tz_class = StaticTzInfo |