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

Method test_tzinfo_now

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

Source from the content-addressed store, hash-verified

5313 self.assertEqual(delta, self.theclass.min - self.theclass.max)
5314
5315 def test_tzinfo_now(self):
5316 meth = self.theclass.now
5317 # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
5318 base = meth()
5319 # Try with and without naming the keyword.
5320 off42 = FixedOffset(42, "42")
5321 another = meth(off42)
5322 again = meth(tz=off42)
5323 self.assertIs(another.tzinfo, again.tzinfo)
5324 self.assertEqual(another.utcoffset(), timedelta(minutes=42))
5325 # Bad argument with and w/o naming the keyword.
5326 self.assertRaises(TypeError, meth, 16)
5327 self.assertRaises(TypeError, meth, tzinfo=16)
5328 # Bad keyword name.
5329 self.assertRaises(TypeError, meth, tinfo=off42)
5330 # Too many args.
5331 self.assertRaises(TypeError, meth, off42, off42)
5332
5333 # We don't know which time zone we're in, and don't have a tzinfo
5334 # class to represent it, so seeing whether a tz argument actually
5335 # does a conversion is tricky.
5336 utc = FixedOffset(0, "utc", 0)
5337 for weirdtz in [FixedOffset(timedelta(hours=15, minutes=58), "weirdtz", 0),
5338 timezone(timedelta(hours=15, minutes=58), "weirdtz"),]:
5339 for dummy in range(3):
5340 now = datetime.now(weirdtz)
5341 self.assertIs(now.tzinfo, weirdtz)
5342 with self.assertWarns(DeprecationWarning):
5343 utcnow = datetime.utcnow().replace(tzinfo=utc)
5344 now2 = utcnow.astimezone(weirdtz)
5345 if abs(now - now2) < timedelta(seconds=30):
5346 break
5347 # Else the code is broken, or more than 30 seconds passed between
5348 # calls; assuming the latter, just try again.
5349 else:
5350 # Three strikes and we're out.
5351 self.fail("utcnow(), now(tz), or astimezone() may be broken")
5352
5353 def test_tzinfo_fromtimestamp(self):
5354 import time

Callers

nothing calls this directly

Calls 15

timedeltaClass · 0.90
timezoneClass · 0.90
methFunction · 0.85
FixedOffsetClass · 0.85
absFunction · 0.85
nowMethod · 0.80
assertWarnsMethod · 0.80
utcnowMethod · 0.80
astimezoneMethod · 0.80
assertIsMethod · 0.45
assertEqualMethod · 0.45
utcoffsetMethod · 0.45

Tested by

no test coverage detected