[Zope3-checkins] CVS: Zope3/src/datetime/tests - test_datetime.py:1.9

Tim Peters tim.one@comcast.net
Tue, 31 Dec 2002 01:06:57 -0500


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

Modified Files:
	test_datetime.py 
Log Message:
A new, and much hairier, implementation of astimezone(), building on
an idea from Guido.  This restores that the datetime implementation
never passes a datetime d to a tzinfo method unless d.tzinfo is the
tzinfo instance whose method is being called.  That in turn allows
enormous simplifications in user-written tzinfo classes (see the Python
sandbox US.py and EU.py for fully fleshed-out examples).

d.astimezone(tz) also raises ValueError now if d lands in the one hour
of the year that can't be expressed in tz (this can happen iff tz models
both standard and daylight time).  That it used to return a nonsense
result always ate at me, and it turned out that it seemed impossible to
force a consistent nonsense result under the new implementation (which
doesn't know anything about how tzinfo classes implement their methods --
it can only infer properties indirectly).

Doc changes will have to wait for tomorrow.  Ditto getting the C
implementation back in synch.


=== Zope3/src/datetime/tests/test_datetime.py 1.8 => 1.9 ===
--- Zope3/src/datetime/tests/test_datetime.py:1.8	Mon Dec 30 14:45:53 2002
+++ Zope3/src/datetime/tests/test_datetime.py	Tue Dec 31 01:06:56 2002
@@ -2570,16 +2570,7 @@
             # An exception instead may be sensible here, in one or more of
             # the cases.
             return ZERO
-
-        convert_endpoints_to_utc = False
-        if dt.tzinfo is not self:
-            # Convert dt to UTC.
-            offset = dt.utcoffset()
-            if offset is None:
-                # Again, an exception instead may be sensible.
-                return ZERO
-            convert_endpoints_to_utc = True
-            dt -= offset
+        assert dt.tzinfo is self
 
         # Find first Sunday in April.
         start = first_sunday_on_or_after(DSTSTART.replace(year=dt.year))
@@ -2589,10 +2580,6 @@
         end = first_sunday_on_or_after(DSTEND.replace(year=dt.year))
         assert end.weekday() == 6 and end.month == 10 and end.day >= 25
 
-        if convert_endpoints_to_utc:
-            start -= self.stdoffset    # start is in std time
-            end -= self.stdoffset + HOUR # end is in DST time
-
         # Can't compare naive to aware objects, so strip the timezone from
         # dt first.
         if start <= dt.astimezone(None) < end:
@@ -2664,7 +2651,6 @@
                 # standard time.  The hour 1:MM:SS standard time ==
                 # 2:MM:SS daylight time can't be expressed in local time.
                 nexthour_utc = asutc + HOUR
-                nexthour_tz = nexthour_utc.astimezone(tz)
                 if during.date() == dstoff.date() and during.hour == 1:
                     # We're in the hour before DST ends.  The hour after
                     # is ineffable.
@@ -2678,8 +2664,10 @@
                     # That's correct, too, *if* 1:MM:SS were taken as
                     # being standard time.  But it's not -- on this day
                     # it's taken as daylight time.
-                    self.assertEqual(during, nexthour_tz)
+                    self.assertRaises(ValueError,
+                                      nexthour_utc.astimezone, tz)
                 else:
+                    nexthour_tz = nexthour_utc.astimezone(utc)
                     self.assertEqual(nexthour_tz - during, HOUR)
 
             for outside in dston - delta, dstoff, dstoff + delta: