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

Method test_astimezone

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

Source from the content-addressed store, hash-verified

3271
3272 @support.run_with_tz('EDT4')
3273 def test_astimezone(self):
3274 dt = self.theclass.now()
3275 f = FixedOffset(44, "0044")
3276 dt_utc = dt.replace(tzinfo=timezone(timedelta(hours=-4), 'EDT'))
3277 self.assertEqual(dt.astimezone(), dt_utc) # naive
3278 self.assertRaises(TypeError, dt.astimezone, f, f) # too many args
3279 self.assertRaises(TypeError, dt.astimezone, dt) # arg wrong type
3280 dt_f = dt.replace(tzinfo=f) + timedelta(hours=4, minutes=44)
3281 self.assertEqual(dt.astimezone(f), dt_f) # naive
3282 self.assertEqual(dt.astimezone(tz=f), dt_f) # naive
3283
3284 class Bogus(tzinfo):
3285 def utcoffset(self, dt): return None
3286 def dst(self, dt): return timedelta(0)
3287 bog = Bogus()
3288 self.assertRaises(ValueError, dt.astimezone, bog) # naive
3289 self.assertEqual(dt.replace(tzinfo=bog).astimezone(f), dt_f)
3290
3291 class AlsoBogus(tzinfo):
3292 def utcoffset(self, dt): return timedelta(0)
3293 def dst(self, dt): return None
3294 alsobog = AlsoBogus()
3295 self.assertRaises(ValueError, dt.astimezone, alsobog) # also naive
3296
3297 class Broken(tzinfo):
3298 def utcoffset(self, dt): return 1
3299 def dst(self, dt): return 1
3300 broken = Broken()
3301 dt_broken = dt.replace(tzinfo=broken)
3302 with self.assertRaises(TypeError):
3303 dt_broken.astimezone()
3304
3305 def test_subclass_datetime(self):
3306

Callers

nothing calls this directly

Calls 11

timezoneClass · 0.90
timedeltaClass · 0.90
FixedOffsetClass · 0.85
AlsoBogusClass · 0.85
nowMethod · 0.80
astimezoneMethod · 0.80
BogusClass · 0.70
BrokenClass · 0.70
replaceMethod · 0.45
assertEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected