| 5823 | utc_fake = FixedOffset(-12*60, "UTCfake", 0) |
| 5824 | |
| 5825 | class TestTimezoneConversions(unittest.TestCase): |
| 5826 | # The DST switch times for 2002, in std time. |
| 5827 | dston = datetime(2002, 4, 7, 2) |
| 5828 | dstoff = datetime(2002, 10, 27, 1) |
| 5829 | |
| 5830 | theclass = datetime |
| 5831 | |
| 5832 | # Check a time that's inside DST. |
| 5833 | def checkinside(self, dt, tz, utc, dston, dstoff): |
| 5834 | self.assertEqual(dt.dst(), HOUR) |
| 5835 | |
| 5836 | # Conversion to our own timezone is always an identity. |
| 5837 | self.assertEqual(dt.astimezone(tz), dt) |
| 5838 | |
| 5839 | asutc = dt.astimezone(utc) |
| 5840 | there_and_back = asutc.astimezone(tz) |
| 5841 | |
| 5842 | # Conversion to UTC and back isn't always an identity here, |
| 5843 | # because there are redundant spellings (in local time) of |
| 5844 | # UTC time when DST begins: the clock jumps from 1:59:59 |
| 5845 | # to 3:00:00, and a local time of 2:MM:SS doesn't really |
| 5846 | # make sense then. The classes above treat 2:MM:SS as |
| 5847 | # daylight time then (it's "after 2am"), really an alias |
| 5848 | # for 1:MM:SS standard time. The latter form is what |
| 5849 | # conversion back from UTC produces. |
| 5850 | if dt.date() == dston.date() and dt.hour == 2: |
| 5851 | # We're in the redundant hour, and coming back from |
| 5852 | # UTC gives the 1:MM:SS standard-time spelling. |
| 5853 | self.assertEqual(there_and_back + HOUR, dt) |
| 5854 | # Although during was considered to be in daylight |
| 5855 | # time, there_and_back is not. |
| 5856 | self.assertEqual(there_and_back.dst(), ZERO) |
| 5857 | # They're the same times in UTC. |
| 5858 | self.assertEqual(there_and_back.astimezone(utc), |
| 5859 | dt.astimezone(utc)) |
| 5860 | else: |
| 5861 | # We're not in the redundant hour. |
| 5862 | self.assertEqual(dt, there_and_back) |
| 5863 | |
| 5864 | # Because we have a redundant spelling when DST begins, there is |
| 5865 | # (unfortunately) an hour when DST ends that can't be spelled at all in |
| 5866 | # local time. When DST ends, the clock jumps from 1:59 back to 1:00 |
| 5867 | # again. The hour 1:MM DST has no spelling then: 1:MM is taken to be |
| 5868 | # standard time. 1:MM DST == 0:MM EST, but 0:MM is taken to be |
| 5869 | # daylight time. The hour 1:MM daylight == 0:MM standard can't be |
| 5870 | # expressed in local time. Nevertheless, we want conversion back |
| 5871 | # from UTC to mimic the local clock's "repeat an hour" behavior. |
| 5872 | nexthour_utc = asutc + HOUR |
| 5873 | nexthour_tz = nexthour_utc.astimezone(tz) |
| 5874 | if dt.date() == dstoff.date() and dt.hour == 0: |
| 5875 | # We're in the hour before the last DST hour. The last DST hour |
| 5876 | # is ineffable. We want the conversion back to repeat 1:MM. |
| 5877 | self.assertEqual(nexthour_tz, dt.replace(hour=1)) |
| 5878 | nexthour_utc += HOUR |
| 5879 | nexthour_tz = nexthour_utc.astimezone(tz) |
| 5880 | self.assertEqual(nexthour_tz, dt.replace(hour=1)) |
| 5881 | else: |
| 5882 | self.assertEqual(nexthour_tz - dt, HOUR) |
nothing calls this directly
no test coverage detected
searching dependent graphs…