[Python-checkins] python/dist/src/Lib/test test_datetime.py,1.21,1.22

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Fri, 03 Jan 2003 22:03:17 -0800


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1:/tmp/cvs-serv8819/python/Lib/test

Modified Files:
	test_datetime.py 
Log Message:
A new implementation of astimezone() that does what we agreed on in all
cases, plus even tougher tests of that.  This implementation follows
the correctness proof very closely, and should also be quicker (yes,
I wrote the proof before the code, and the code proves the proof <wink>).


Index: test_datetime.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_datetime.py,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -d -r1.21 -r1.22
*** test_datetime.py	2 Jan 2003 21:28:07 -0000	1.21
--- test_datetime.py	4 Jan 2003 06:03:15 -0000	1.22
***************
*** 2593,2597 ****
  # For better test coverage, we want another flavor of UTC that's west of
  # the Eastern and Pacific timezones.
! utc_fake = FixedOffset(-12, "UTCfake", 0)
  
  class TestTimezoneConversions(unittest.TestCase):
--- 2593,2597 ----
  # For better test coverage, we want another flavor of UTC that's west of
  # the Eastern and Pacific timezones.
! utc_fake = FixedOffset(-12*60, "UTCfake", 0)
  
  class TestTimezoneConversions(unittest.TestCase):
***************
*** 2644,2666 ****
          # 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
          if dt.date() == dstoff.date() and dt.hour == 1:
              # We're in the hour before DST ends.  The hour after
!             # is ineffable.
!             # For concreteness, picture Eastern.  during is of
!             # the form 1:MM:SS, it's daylight time, so that's
!             # 5:MM:SS UTC.  Adding an hour gives 6:MM:SS UTC.
!             # Daylight time ended at 2+4 == 6:00:00 UTC, so
!             # 6:MM:SS is (correctly) taken to be standard time.
!             # But standard time is at offset -5, and that maps
!             # right back to the 1:MM:SS Eastern we started with.
!             # 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.assertRaises(ValueError,
!                               nexthour_utc.astimezone, tz)
          else:
!             nexthour_tz = nexthour_utc.astimezone(utc)
!             self.assertEqual(nexthour_tz - dt, HOUR)
  
      # Check a time that's outside DST.
--- 2644,2658 ----
          # standard time.  The hour 1:MM:SS standard time ==
          # 2:MM:SS daylight time can't be expressed in local time.
+         # Nevertheless, we want conversion back from UTC to mimic
+         # the local clock's "repeat an hour" behavior.
          nexthour_utc = asutc + HOUR
+         nexthour_tz = nexthour_utc.astimezone(tz)
          if dt.date() == dstoff.date() and dt.hour == 1:
              # We're in the hour before DST ends.  The hour after
!             # is ineffable.  We want the conversion back to repeat 1:MM.
!             expected_diff = ZERO
          else:
!             expected_diff = HOUR
!         self.assertEqual(nexthour_tz - dt, expected_diff)
  
      # Check a time that's outside DST.
***************
*** 2739,2742 ****
--- 2731,2759 ----
          got = sixutc.astimezone(Eastern).astimezone(None)
          self.assertEqual(expected, got)
+ 
+         # Now on the day DST ends, we want "repeat an hour" behavior.
+         #  UTC  4:MM  5:MM  6:MM  7:MM  checking these
+         #  EST 23:MM  0:MM  1:MM  2:MM
+         #  EDT  0:MM  1:MM  2:MM  3:MM
+         # wall  0:MM  1:MM  1:MM  2:MM  against these
+         for utc in utc_real, utc_fake:
+             for tz in Eastern, Pacific:
+                 first_std_hour = self.dstoff - timedelta(hours=3) # 23:MM
+                 # Convert that to UTC.
+                 first_std_hour -= tz.utcoffset(None)
+                 # Adjust for possibly fake UTC.
+                 asutc = first_std_hour + utc.utcoffset(None)
+                 # First UTC hour to convert; this is 4:00 when utc=utc_real &
+                 # tz=Eastern.
+                 asutcbase = asutc.replace(tzinfo=utc)
+                 for tzhour in (0, 1, 1, 2):
+                     expectedbase = self.dstoff.replace(hour=tzhour)
+                     for minute in 0, 30, 59:
+                         expected = expectedbase.replace(minute=minute)
+                         asutc = asutcbase.replace(minute=minute)
+                         astz = asutc.astimezone(tz)
+                         self.assertEqual(astz.replace(tzinfo=None), expected)
+                     asutcbase += HOUR
+ 
  
      def test_bogus_dst(self):