[Zope3-checkins] CVS: Zope3/src/zope/app - datetimeutils.py:1.5
Jim Fulton
jim@zope.com
Thu, 9 Jan 2003 13:30:20 -0500
Update of /cvs-repository/Zope3/src/zope/app
In directory cvs.zope.org:/tmp/cvs-serv25358/src/zope/app
Modified Files:
datetimeutils.py
Log Message:
Made zope.appp.datetimeutils.tzinfo objects pickleable.
Also arranged that there would be only one tzinfo object for a
given offset.
=== Zope3/src/zope/app/datetimeutils.py 1.4 => 1.5 ===
--- Zope3/src/zope/app/datetimeutils.py:1.4 Wed Jan 8 15:18:26 2003
+++ Zope3/src/zope/app/datetimeutils.py Thu Jan 9 13:29:48 2003
@@ -954,30 +954,53 @@
parse = parser.parse
time = parser.time
+######################################################################
+# Time-zone info based soley on offsets
+#
+# Share tzinfos for the same offset
+
from datetime import tzinfo as _tzinfo, timedelta as _timedelta
-_ZERO = _timedelta(0)
-class tzinfo(_tzinfo):
- __slots__ = ('offset', )
+class _tzinfo(_tzinfo):
- def __init__(self, offset):
- if isinstance(offset, int):
- offset = _timedelta(minutes=offset)
- self.offset = offset
+ def __init__(self, minutes):
+ if abs(minutes) > 1439:
+ raise ValueError("Time-zone offset is too large,", minutes)
+ self.__minutes = minutes
+ self.__offset = _timedelta(minutes=minutes)
+
+ def utcoffset(self, dt):
+ return self.__offset
+
+ def __reduce__(self):
+ return tzinfo, (self.__minutes, )
+
+ def dst(self, dt):
+ return None
+
+ def tzname(self, dt):
+ return None
- def utcoffset(self, dt=None):
- return self.offset
+ def __repr__(self):
+ return 'tzinfo(%d)' % self.__minutes
- __getstate__ = utcoffset
- __setstate__ = __init__
- def dst(self, dt): return _ZERO
- def tzname(self, dt): return ''
+def tzinfo(offset, _tzinfos = {}):
- def __repr__(self):
- minutes = self.offset.days * 24 * 60 + self.offset.seconds // 60
- return 'tzinfo(%d)' % minutes
+ info = _tzinfos.get(offset)
+ if info is None:
+ # We haven't seen this one before. we need to save it.
+
+ # Use setdefault to avoid a race condition and make sure we have
+ # only one
+ info = _tzinfos.setdefault(offset, _tzinfo(offset))
+
+ return info
+
+tzinfo.__safe_for_unpickling__ = True
+#
+######################################################################
from datetime import datetime as _datetime
def parseDatetimetz(string):