[Zope3-checkins] CVS: Zope3/src/datetime - _datetime.py:1.1.2.3 doc.txt:1.1.2.4

Tim Peters tim.one@comcast.net
Wed, 25 Dec 2002 01:52:19 -0500


Update of /cvs-repository/Zope3/src/datetime
In directory cvs.zope.org:/tmp/cvs-serv24006/src/datetime

Modified Files:
      Tag: NameGeddon-branch
	_datetime.py doc.txt 
Log Message:
Implemented datetime.astimezone() and datetimetz.astimezone().  Cleared
some XXX comments.


=== Zope3/src/datetime/_datetime.py 1.1.2.2 => 1.1.2.3 ===
--- Zope3/src/datetime/_datetime.py:1.1.2.2	Tue Dec 24 00:53:15 2002
+++ Zope3/src/datetime/_datetime.py	Wed Dec 25 01:51:48 2002
@@ -8,8 +8,8 @@
 import time as _time
 import math as _math
 
-MINYEAR = 1     # XXX The design doc says 0
-MAXYEAR = 9999  # XXX The design doc says 65535
+MINYEAR = 1
+MAXYEAR = 9999
 
 # Utility functions, adapted from Python's Demo/classes/Dates.py, which
 # also assumes the current Gregorian calendar indefinitely extended in
@@ -1373,6 +1373,11 @@
         return time(self.__hour, self.__minute, self.__second,
                     self.__microsecond)
 
+    def astimezone(self, tz):
+        _check_tzinfo_arg(tz)
+        temp = datetimetz.combine(self.date(), self.time())
+        return temp.replace(tzinfo=tz)
+
     def __cmp__(self, other):
         "Three-way comparison."
         if isinstance(other, datetime):
@@ -1490,7 +1495,7 @@
 
 class datetimetz(datetime):
 
-    # XXX needs docstrings and conversion APIs
+    # XXX needs docstrings
     # See http://www.zope.org/Members/fdrake/DateTimeWiki/TimeZoneInfo
 
     def __init__(self, year, month, day, hour=0, minute=0, second=0,
@@ -1577,6 +1582,18 @@
         return datetimetz(year, month, day, hour, minute, second,
                           microsecond, tzinfo)
 
+    def astimezone(self, tz):
+        _check_tzinfo_arg(tz)
+        offset = self.utcoffset()
+        if offset is not None and tz is not None:
+            newoffset = tz.utcoffset(self)
+            if newoffset is not None:
+                if not isinstance(newoffset, timedelta):
+                    newoffset = timedelta(minutes=newoffset)
+                diff = offset - newoffset
+                self -= diff # this can overflow; can't be helped
+        return self.replace(tzinfo=tz)
+
     def isoformat(self, sep='T'):
         s = super(datetimetz, self).isoformat(sep)
         off = self._utcoffset()
@@ -1741,11 +1758,7 @@
 # implementation had to get much trickier, and the code following emulates
 # what the C code had to do, so that pickles produced by the Python
 # implementation can be read by the C implementation, and vice versa.
-# XXX This isn't entirely successful yet.  The Python implementation can
-# XXX read pickles written by the C implementation now, but the
-# XXX C implementation can't read timetz, datetimetz, or timedelta
-# XXX pickles written by the Python implementation.  See doc.txt.
-
+ 
 def _date_pickler(date):
     state = date.__getstate__()
     return _date_unpickler, (state,)


=== Zope3/src/datetime/doc.txt 1.1.2.3 => 1.1.2.4 ===
--- Zope3/src/datetime/doc.txt:1.1.2.3	Tue Dec 24 11:36:42 2002
+++ Zope3/src/datetime/doc.txt	Wed Dec 25 01:51:48 2002
@@ -622,6 +622,10 @@
     Return a datetime with the same fields as self, except for those
     members given in the argument list.
 
+  - astimezone(tz)
+    Return a datetimetz with the same date and time fields, and with
+    tzinfo=tz.  tz must be an instance of a tzinfo subclass.
+
   - timetuple()
     Return a 9-element tuple of the form returned by time.localtime().
     The DST flag is -1.   d.timetuple() is equivalent to
@@ -1120,6 +1124,20 @@
     members given in the argument list.  Note that the tzinfo argument
     defaults to True instead of to None.  This is so you can specify
     tzinfo=None to remove a datetimetz's tzinfo member.
+
+  - astimezone(tz)
+    Return a datetimetz with new tzinfo member.  tz must be an instance
+    of a tzinfo subclass.  If self is naive, or if tz.utcoffset(self)
+    returns None, self.astimezone(tz) is equivalent to
+    self.replace(tzinfo=tz):  a new timezone object is attached without
+    any conversion of date or time fields.  If self is aware and
+    tz.utcoffset(self) does not return None, the date and time fields
+    are adjusted so that the result is local time in timezone tz,
+    representing the same UTC time as self.  self.astimezone(tz) is then
+    equivalent to
+        (self - (self.utcoffset() - tz.utcoffset(self)).replace(tzinfo=tz)
+    where the result of tz.uctcoffset(self) is coerced to a timedelta if
+    needed.
 
   - timetuple()
     Like datetime.timetuple(), but sets the tm_isdst flag according to