summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>2010-10-14 17:03:51 (GMT)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>2010-10-14 17:03:51 (GMT)
commit1bcbaab15a38ce23cfa4e5ddfee507b75b7cc0b4 (patch)
tree92b95c8558e6a10a2545a7f7c1517e8d84466f66 /Lib
parent5bc4fa7a4947a39cbfdd3dd2ea1019bfa0f93085 (diff)
downloadcpython-1bcbaab15a38ce23cfa4e5ddfee507b75b7cc0b4.zip
cpython-1bcbaab15a38ce23cfa4e5ddfee507b75b7cc0b4.tar.gz
cpython-1bcbaab15a38ce23cfa4e5ddfee507b75b7cc0b4.tar.bz2
Issue 9183: Intern UTC timezone.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/datetime.py39
-rw-r--r--Lib/test/datetimetester.py4
2 files changed, 26 insertions, 17 deletions
diff --git a/Lib/datetime.py b/Lib/datetime.py
index d640f75..29ffe24 100644
--- a/Lib/datetime.py
+++ b/Lib/datetime.py
@@ -1784,24 +1784,31 @@ class timezone(tzinfo):
# Sentinel value to disallow None
_Omitted = object()
- def __init__(self, offset, name=_Omitted):
- if name is self._Omitted:
+ def __new__(cls, offset, name=_Omitted):
+ if not isinstance(offset, timedelta):
+ raise TypeError("offset must be a timedelta")
+ if name is cls._Omitted:
+ if not offset:
+ return cls.utc
name = None
elif not isinstance(name, str):
raise TypeError("name must be a string")
- if isinstance(offset, timedelta):
- if self._minoffset <= offset <= self._maxoffset:
- if (offset.microseconds != 0 or
- offset.seconds % 60 != 0):
- raise ValueError("offset must be whole"
- " number of minutes")
- self._offset = offset
- else:
- raise ValueError("offset out of range")
- else:
- raise TypeError("offset must be timedelta")
+ if not cls._minoffset <= offset <= cls._maxoffset:
+ raise ValueError("offset must be a timedelta"
+ " strictly between -timedelta(hours=24) and"
+ " timedelta(hours=24).")
+ if (offset.microseconds != 0 or
+ offset.seconds % 60 != 0):
+ raise ValueError("offset must be a timedelta"
+ " representing a whole number of minutes")
+ return cls._create(offset, name)
+ @classmethod
+ def _create(cls, offset, name=None):
+ self = tzinfo.__new__(cls)
+ self._offset = offset
self._name = name
+ return self
def __getinitargs__(self):
"""pickle support"""
@@ -1879,9 +1886,9 @@ class timezone(tzinfo):
minutes = rest // timedelta(minutes=1)
return 'UTC{}{:02d}:{:02d}'.format(sign, hours, minutes)
-timezone.utc = timezone(timedelta(0))
-timezone.min = timezone(timezone._minoffset)
-timezone.max = timezone(timezone._maxoffset)
+timezone.utc = timezone._create(timedelta(0))
+timezone.min = timezone._create(timezone._minoffset)
+timezone.max = timezone._create(timezone._maxoffset)
"""
Some time zone algebra. For a datetime x, let
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index 0ee544b..e91eb67 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -176,7 +176,9 @@ class TestTimeZone(unittest.TestCase):
def test_constructor(self):
- self.assertEqual(timezone.utc, timezone(timedelta(0)))
+ self.assertIs(timezone.utc, timezone(timedelta(0)))
+ self.assertIsNot(timezone.utc, timezone(timedelta(0), 'UTC'))
+ self.assertEqual(timezone.utc, timezone(timedelta(0), 'UTC'))
# invalid offsets
for invalid in [timedelta(microseconds=1), timedelta(1, 1),
timedelta(seconds=1), timedelta(1), -timedelta(1)]: