MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_fromutc

Method test_fromutc

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

Source from the content-addressed store, hash-verified

5831 self.assertRaises(ValueError, dt.astimezone, tricky_notok())
5832
5833 def test_fromutc(self):
5834 self.assertRaises(TypeError, Eastern.fromutc) # not enough args
5835 now = datetime.now(tz=utc_real)
5836 self.assertRaises(ValueError, Eastern.fromutc, now) # wrong tzinfo
5837 now = now.replace(tzinfo=Eastern) # insert correct tzinfo
5838 enow = Eastern.fromutc(now) # doesn't blow up
5839 self.assertEqual(enow.tzinfo, Eastern) # has right tzinfo member
5840 self.assertRaises(TypeError, Eastern.fromutc, now, now) # too many args
5841 self.assertRaises(TypeError, Eastern.fromutc, date.today()) # wrong type
5842
5843 # Always converts UTC to standard time.
5844 class FauxUSTimeZone(USTimeZone):
5845 def fromutc(self, dt):
5846 return dt + self.stdoffset
5847 FEastern = FauxUSTimeZone(-5, "FEastern", "FEST", "FEDT")
5848
5849 # UTC 4:MM 5:MM 6:MM 7:MM 8:MM 9:MM
5850 # EST 23:MM 0:MM 1:MM 2:MM 3:MM 4:MM
5851 # EDT 0:MM 1:MM 2:MM 3:MM 4:MM 5:MM
5852
5853 # Check around DST start.
5854 start = self.dston.replace(hour=4, tzinfo=Eastern)
5855 fstart = start.replace(tzinfo=FEastern)
5856 for wall in 23, 0, 1, 3, 4, 5:
5857 expected = start.replace(hour=wall)
5858 if wall == 23:
5859 expected -= timedelta(days=1)
5860 got = Eastern.fromutc(start)
5861 self.assertEqual(expected, got)
5862
5863 expected = fstart + FEastern.stdoffset
5864 got = FEastern.fromutc(fstart)
5865 self.assertEqual(expected, got)
5866
5867 # Ensure astimezone() calls fromutc() too.
5868 got = fstart.replace(tzinfo=utc_real).astimezone(FEastern)
5869 self.assertEqual(expected, got)
5870
5871 start += HOUR
5872 fstart += HOUR
5873
5874 # Check around DST end.
5875 start = self.dstoff.replace(hour=4, tzinfo=Eastern)
5876 fstart = start.replace(tzinfo=FEastern)
5877 for wall in 0, 1, 1, 2, 3, 4:
5878 expected = start.replace(hour=wall)
5879 got = Eastern.fromutc(start)
5880 self.assertEqual(expected, got)
5881
5882 expected = fstart + FEastern.stdoffset
5883 got = FEastern.fromutc(fstart)
5884 self.assertEqual(expected, got)
5885
5886 # Ensure astimezone() calls fromutc() too.
5887 got = fstart.replace(tzinfo=utc_real).astimezone(FEastern)
5888 self.assertEqual(expected, got)
5889
5890 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