MCPcopy Index your code
hub / github.com/python/cpython / test_fromutc

Method test_fromutc

Lib/test/datetimetester.py:6020–6078  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

6018 self.assertRaises(ValueError, dt.astimezone, tricky_notok())
6019
6020 def test_fromutc(self):
6021 self.assertRaises(TypeError, Eastern.fromutc) # not enough args
6022 now = datetime.now(tz=utc_real)
6023 self.assertRaises(ValueError, Eastern.fromutc, now) # wrong tzinfo
6024 now = now.replace(tzinfo=Eastern) # insert correct tzinfo
6025 enow = Eastern.fromutc(now) # doesn't blow up
6026 self.assertEqual(enow.tzinfo, Eastern) # has right tzinfo member
6027 self.assertRaises(TypeError, Eastern.fromutc, now, now) # too many args
6028 self.assertRaises(TypeError, Eastern.fromutc, date.today()) # wrong type
6029
6030 # Always converts UTC to standard time.
6031 class FauxUSTimeZone(USTimeZone):
6032 def fromutc(self, dt):
6033 return dt + self.stdoffset
6034 FEastern = FauxUSTimeZone(-5, "FEastern", "FEST", "FEDT")
6035
6036 # UTC 4:MM 5:MM 6:MM 7:MM 8:MM 9:MM
6037 # EST 23:MM 0:MM 1:MM 2:MM 3:MM 4:MM
6038 # EDT 0:MM 1:MM 2:MM 3:MM 4:MM 5:MM
6039
6040 # Check around DST start.
6041 start = self.dston.replace(hour=4, tzinfo=Eastern)
6042 fstart = start.replace(tzinfo=FEastern)
6043 for wall in 23, 0, 1, 3, 4, 5:
6044 expected = start.replace(hour=wall)
6045 if wall == 23:
6046 expected -= timedelta(days=1)
6047 got = Eastern.fromutc(start)
6048 self.assertEqual(expected, got)
6049
6050 expected = fstart + FEastern.stdoffset
6051 got = FEastern.fromutc(fstart)
6052 self.assertEqual(expected, got)
6053
6054 # Ensure astimezone() calls fromutc() too.
6055 got = fstart.replace(tzinfo=utc_real).astimezone(FEastern)
6056 self.assertEqual(expected, got)
6057
6058 start += HOUR
6059 fstart += HOUR
6060
6061 # Check around DST end.
6062 start = self.dstoff.replace(hour=4, tzinfo=Eastern)
6063 fstart = start.replace(tzinfo=FEastern)
6064 for wall in 0, 1, 1, 2, 3, 4:
6065 expected = start.replace(hour=wall)
6066 got = Eastern.fromutc(start)
6067 self.assertEqual(expected, got)
6068
6069 expected = fstart + FEastern.stdoffset
6070 got = FEastern.fromutc(fstart)
6071 self.assertEqual(expected, got)
6072
6073 # Ensure astimezone() calls fromutc() too.
6074 got = fstart.replace(tzinfo=utc_real).astimezone(FEastern)
6075 self.assertEqual(expected, got)
6076
6077 start += HOUR

Callers

nothing calls this directly

Calls 10

fromutcMethod · 0.95
timedeltaClass · 0.90
FauxUSTimeZoneClass · 0.85
nowMethod · 0.80
todayMethod · 0.80
astimezoneMethod · 0.80
assertRaisesMethod · 0.45
replaceMethod · 0.45
fromutcMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected