[Zope3-checkins] CVS: Zope3/lib/python/datetime - __init__.py:1.6 _datetime.py:1.8

Tim Peters tim.one@comcast.net
Fri, 20 Dec 2002 17:31:08 -0500


Update of /cvs-repository/Zope3/lib/python/datetime
In directory cvs.zope.org:/tmp/cvs-serv2135/lib/python/datetime

Modified Files:
	__init__.py _datetime.py 
Log Message:
Synch with the current version of datetime.py.  This includes a beefed-
up test suite.
Note that tzinfo objects must now derive from datetime.tzinfo.
Note that repr() strings now include the module name, such as
datetime.date or datetime.timetz.  This is for compatibility with the
Python 2.3's C implementation (which, alas, doesn't compile under
Python 2.2.2).
Note that there's a bug in Python 2.2.2 (fixed in CVS) that the test
suite worms around.


=== Zope3/lib/python/datetime/__init__.py 1.5 => 1.6 ===
--- Zope3/lib/python/datetime/__init__.py:1.5	Fri Dec 20 13:53:04 2002
+++ Zope3/lib/python/datetime/__init__.py	Fri Dec 20 17:30:37 2002
@@ -1,14 +1,12 @@
-# Python datetime prototype,
+# Python datetime prototype.
 
-# This package contains the prototype datetime module that will be included
-# in Python 2.3.  We've turned it into a package to make it easier to
-# deal with in CVS for now.  This __init__ file makes the package look
-# like the eventual module.
+# This package contains the prototype datetime Python module whose C
+# version is included in Python 2.3.  We've turned it into a package to
+# make it easier to deal with in CVS for now.  This __init__ file makes the
+# package look like the eventual module.
 
 from _datetime import MINYEAR, MAXYEAR
 from _datetime import timedelta
 from _datetime import time, timetz
 from _datetime import date, datetime, datetimetz
-# XXX Temporary, to allow the tests to pass.  This will be replaced by the
-# XXX C datetime code (and much larger test suite) soon, where it isn't needed.
-from _datetime import _ymd2ord, _ord2ymd, tmxxx
\ No newline at end of file
+from _datetime import tzinfo


=== Zope3/lib/python/datetime/_datetime.py 1.7 => 1.8 === (945/1045 lines abridged)
--- Zope3/lib/python/datetime/_datetime.py:1.7	Mon Dec  2 04:56:11 2002
+++ Zope3/lib/python/datetime/_datetime.py	Fri Dec 20 17:30:37 2002
@@ -149,6 +149,79 @@
                      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
 _DAYNAMES = [None, "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
 
+
+def _build_struct_time(y, m, d, hh, mm, ss, dstflag):
+    wday = (_ymd2ord(y, m, d) + 6) % 7
+    dnum = _days_before_month(m, y) + d
+    return _time.struct_time((y, m, d, hh, mm, ss, wday, dnum, dstflag))
+
+def _format_time(hh, mm, ss, us):
+    # Skip trailing microseconds when us==0.
+    result = "%02d:%02d:%02d" % (hh, mm, ss)
+    if us:
+        result += ".%06d" % us
+    return result
+
+# Correctly substitute for %z and %Z escapes in strftime formats.
+def _wrap_strftime(object, format, timetuple):
+    # Don't call utcoffset() or tzname() unless actually needed.
+    zreplace = None # the string to use for %z
+    Zreplace = None # the string to use for %Z
+
+    # Scan format for %z and %Z escapes, replacing as needed.
+    newformat = []
+    push = newformat.append
+    i, n = 0, len(format)
+    while i < n:
+        ch = format[i]
+        i += 1
+        if ch == '%':
+            if i < n:
+                ch = format[i]
+                i += 1
+                if ch == 'z':
+                    if zreplace is None:
+                        zreplace = ""
+                        if hasattr(object, "utcoffset"):
+                            offset = object.utcoffset()
+                            if offset is not None:
+                                sign = '+'
+                                if offset < 0:
+                                    offset = -offset
+                                    sign = '-'
+                                h, m = divmod(offset, 60)
+                                zreplace = '%c%02d%02d' % (sign, h, m)
+                    assert '%' not in zreplace
+                    newformat.append(zreplace)

[-=- -=- -=- 945 lines omitted -=- -=- -=-]

+    self = datetime(1, 1, 1)
+    self.__setstate__(state)
+    return self
+
+def _time_pickler(t):
+    state = t.__getstate__()
+    return _time_unpickler, (state,)
+
+def _time_unpickler(state):
+    self = time()
+    self.__setstate__(state)
+    return self
+
+def _tzinfo_pickler(tz):
+    return _tzinfo_unpickler, ()
+
+def _tzinfo_unpickler():
+    self = tzinfo()
+    return self
+
+def _timetz_pickler(tz):
+    state = tz.__getstate__()
+    return _timetz_unpickler, (state,)
+
+def _timetz_unpickler(state):
+    self = timetz()
+    self.__setstate__(state)
+    return self
+
+def _datetimetz_pickler(dtz):
+    state = dtz.__getstate__()
+    return _datetimetz_unpickler, (state,)
+
+def _datetimetz_unpickler(state):
+    self = datetimetz(1, 1, 1)
+    self.__setstate__(state)
+    return self
+
+# Register pickle/unpickle functions.
+from copy_reg import pickle
+pickle(date, _date_pickler, _date_unpickler)
+pickle(datetime, _datetime_pickler, _datetime_unpickler)
+pickle(time, _time_pickler, _time_unpickler)
+pickle(tzinfo, _tzinfo_pickler, _tzinfo_unpickler)
+pickle(timetz, _timetz_pickler, _timetz_unpickler)
+pickle(datetimetz, _datetimetz_pickler, _datetimetz_unpickler)
+del pickle
 
 def _test():
     import test_datetime