mirror of
https://github.com/python/cpython.git
synced 2025-09-19 15:10:58 +00:00
Beefed up the timezone conversion test by adding a phony UTC zone that's
west of the US zones getting converted, and also by using Eastern "as if" it were UTC (wrt Pacific), and vice versa.
This commit is contained in:
parent
5c8fef903d
commit
1024bf8364
1 changed files with 81 additions and 67 deletions
|
@ -2571,16 +2571,17 @@ class USTimeZone(tzinfo):
|
||||||
|
|
||||||
Eastern = USTimeZone(-5, "Eastern", "EST", "EDT")
|
Eastern = USTimeZone(-5, "Eastern", "EST", "EDT")
|
||||||
Pacific = USTimeZone(-8, "Pacific", "PST", "PDT")
|
Pacific = USTimeZone(-8, "Pacific", "PST", "PDT")
|
||||||
UTC = FixedOffset(0, "UTC", 0)
|
utc_real = FixedOffset(0, "UTC", 0)
|
||||||
|
# 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):
|
class TestTimezoneConversions(unittest.TestCase):
|
||||||
# The DST switch times for 2002, in local time.
|
# The DST switch times for 2002, in local time.
|
||||||
dston = datetimetz(2002, 4, 7, 2)
|
dston = datetimetz(2002, 4, 7, 2)
|
||||||
dstoff = datetimetz(2002, 10, 27, 2)
|
dstoff = datetimetz(2002, 10, 27, 2)
|
||||||
|
|
||||||
def test_easy(self):
|
def convert_between_tz_and_utc(self, tz, utc):
|
||||||
# Despite the name of this test, the endcases are excruciating.
|
|
||||||
for tz in Eastern, Pacific:
|
|
||||||
dston = self.dston.replace(tzinfo=tz)
|
dston = self.dston.replace(tzinfo=tz)
|
||||||
dstoff = self.dstoff.replace(tzinfo=tz)
|
dstoff = self.dstoff.replace(tzinfo=tz)
|
||||||
for delta in (timedelta(weeks=13),
|
for delta in (timedelta(weeks=13),
|
||||||
|
@ -2588,9 +2589,10 @@ class TestTimezoneConversions(unittest.TestCase):
|
||||||
HOUR,
|
HOUR,
|
||||||
timedelta(minutes=1),
|
timedelta(minutes=1),
|
||||||
timedelta(microseconds=1)):
|
timedelta(microseconds=1)):
|
||||||
|
|
||||||
for during in dston, dston + delta, dstoff - delta:
|
for during in dston, dston + delta, dstoff - delta:
|
||||||
self.assertEqual(during.dst(), HOUR)
|
self.assertEqual(during.dst(), HOUR)
|
||||||
asutc = during.astimezone(UTC)
|
asutc = during.astimezone(utc)
|
||||||
there_and_back = asutc.astimezone(tz)
|
there_and_back = asutc.astimezone(tz)
|
||||||
|
|
||||||
# Conversion to UTC and back isn't always an identity here,
|
# Conversion to UTC and back isn't always an identity here,
|
||||||
|
@ -2609,8 +2611,8 @@ class TestTimezoneConversions(unittest.TestCase):
|
||||||
# time, there_and_back is not.
|
# time, there_and_back is not.
|
||||||
self.assertEqual(there_and_back.dst(), ZERO)
|
self.assertEqual(there_and_back.dst(), ZERO)
|
||||||
# They're the same times in UTC.
|
# They're the same times in UTC.
|
||||||
self.assertEqual(there_and_back.astimezone(UTC),
|
self.assertEqual(there_and_back.astimezone(utc),
|
||||||
during.astimezone(UTC))
|
during.astimezone(utc))
|
||||||
else:
|
else:
|
||||||
# We're not in the redundant hour.
|
# We're not in the redundant hour.
|
||||||
self.assertEqual(during, there_and_back)
|
self.assertEqual(during, there_and_back)
|
||||||
|
@ -2644,9 +2646,21 @@ class TestTimezoneConversions(unittest.TestCase):
|
||||||
|
|
||||||
for outside in dston - delta, dstoff, dstoff + delta:
|
for outside in dston - delta, dstoff, dstoff + delta:
|
||||||
self.assertEqual(outside.dst(), ZERO)
|
self.assertEqual(outside.dst(), ZERO)
|
||||||
there_and_back = outside.astimezone(UTC).astimezone(tz)
|
there_and_back = outside.astimezone(utc).astimezone(tz)
|
||||||
self.assertEqual(outside, there_and_back)
|
self.assertEqual(outside, there_and_back)
|
||||||
|
|
||||||
|
def test_easy(self):
|
||||||
|
# Despite the name of this test, the endcases are excruciating.
|
||||||
|
self.convert_between_tz_and_utc(Eastern, utc_real)
|
||||||
|
self.convert_between_tz_and_utc(Pacific, utc_real)
|
||||||
|
self.convert_between_tz_and_utc(Eastern, utc_fake)
|
||||||
|
self.convert_between_tz_and_utc(Pacific, utc_fake)
|
||||||
|
# The next is really dancing near the edge. It works because
|
||||||
|
# Pacific and Eastern are far enough apart that their "problem
|
||||||
|
# hours" don't overlap.
|
||||||
|
self.convert_between_tz_and_utc(Eastern, Pacific)
|
||||||
|
self.convert_between_tz_and_utc(Pacific, Eastern)
|
||||||
|
|
||||||
|
|
||||||
def test_suite():
|
def test_suite():
|
||||||
allsuites = [unittest.makeSuite(klass, 'test')
|
allsuites = [unittest.makeSuite(klass, 'test')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue