[Zope3-checkins] CVS: Zope3/src/datetime - _datetime.py:1.10

Tim Peters tim.one@comcast.net
Tue, 31 Dec 2002 23:16:00 -0500


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

Modified Files:
	_datetime.py 
Log Message:
The failure of the last-second addition to the timezone coversion test is
understood now:  it can't work.  Added comments explaining why (it's "the
usual"-- unrepresentable hours in local time --but in a slightly different
guise).

Added an optimization to astimezone().  This will pay off more in the C
implementation (which is already optimized to work with offsets as C
ints internally, instead of with timedelta objects; this will let it
replace a datetimetz comparison call with a couple of int operations; in
the Python implementation it trades away a datetimetz comparison call
for a couple of timedelta operations).


=== Zope3/src/datetime/_datetime.py 1.9 => 1.10 ===
--- Zope3/src/datetime/_datetime.py:1.9	Tue Dec 31 10:57:42 2002
+++ Zope3/src/datetime/_datetime.py	Tue Dec 31 23:15:29 2002
@@ -1638,7 +1638,8 @@
         if otoff is None:
             return other
 
-        other += otoff - myoff
+        total_added_to_other = otoff - myoff
+        other += total_added_to_other
         # If tz is a fixed-offset class, we're done, but we can't know
         # whether it is.  If it's a DST-aware class, and we're not near a
         # DST boundary, we're also done.  If we crossed a DST boundary,
@@ -1649,7 +1650,9 @@
         if newoff is None:
             self._inconsistent_utcoffset_error()
         if newoff != otoff:
-            other += newoff - otoff
+            delta = newoff - otoff
+            total_added_to_other += delta
+            other += delta
             otoff = other.utcoffset()
             if otoff is None:
                 self._inconsistent_utcoffset_error()
@@ -1662,17 +1665,30 @@
         altoff = alt.utcoffset()
         if altoff is None:
             self._inconsistent_utcoffset_error()
-        # Are alt and other really the same time?  alt == other iff
+        # Are alt and other really the same time?  They are iff
         # alt - altoff == other - otoff, iff
         # (other - _HOUR) - altoff = other - otoff, iff
         # otoff - altoff == _HOUR
+        # Note that the Python comparison "alt == other" would return false,
+        # though, because they have same tzinfo member, and utcoffset() is
+        # ignored when comparing times w/ the same tzinfo.
         diff = otoff - altoff
+
+        # Enable the assert if you're dubious; it's expensive.
+        ##assert ((diff == _HOUR) ==
+        ##        (alt.replace(tzinfo=None) - alt.utcoffset() ==
+        ##         other.replace(tzinfo=None) - other.utcoffset()))
         if diff == _HOUR:
             return alt      # use the local time that makes sense
 
         # There's still a problem with the unspellable (in local time)
-        # hour after DST ends.
-        if self == other:
+        # hour after DST ends.  other's local time now is
+        # self + total_added_to_other, so self == other iff
+        # self - myoff = other - otoff, iff
+        # self - myoff = self + total_added_to_other - otoff, iff
+        # total_added_to_other == otoff - myoff
+        ##assert (self == other) == (total_added_to_other == otoff - myoff)
+        if total_added_to_other == otoff - myoff:
             return other
         # Else there's no way to spell self in zone other.tz.
         raise ValueError("astimezone():  the source datetimetz can't be "