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

Method test_tz_aware_arithmetic

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

Source from the content-addressed store, hash-verified

5248 self.assertEqual(dt.timetz(), time(18, 45, 3, 1234, tzinfo=met))
5249
5250 def test_tz_aware_arithmetic(self):
5251 now = self.theclass.now()
5252 tz55 = FixedOffset(-330, "west 5:30")
5253 timeaware = now.time().replace(tzinfo=tz55)
5254 nowaware = self.theclass.combine(now.date(), timeaware)
5255 self.assertIs(nowaware.tzinfo, tz55)
5256 self.assertEqual(nowaware.timetz(), timeaware)
5257
5258 # Can't mix aware and non-aware.
5259 self.assertRaises(TypeError, lambda: now - nowaware)
5260 self.assertRaises(TypeError, lambda: nowaware - now)
5261
5262 # And adding datetime's doesn't make sense, aware or not.
5263 self.assertRaises(TypeError, lambda: now + nowaware)
5264 self.assertRaises(TypeError, lambda: nowaware + now)
5265 self.assertRaises(TypeError, lambda: nowaware + nowaware)
5266
5267 # Subtracting should yield 0.
5268 self.assertEqual(now - now, timedelta(0))
5269 self.assertEqual(nowaware - nowaware, timedelta(0))
5270
5271 # Adding a delta should preserve tzinfo.
5272 delta = timedelta(weeks=1, minutes=12, microseconds=5678)
5273 nowawareplus = nowaware + delta
5274 self.assertIs(nowaware.tzinfo, tz55)
5275 nowawareplus2 = delta + nowaware
5276 self.assertIs(nowawareplus2.tzinfo, tz55)
5277 self.assertEqual(nowawareplus, nowawareplus2)
5278
5279 # that - delta should be what we started with, and that - what we
5280 # started with should be delta.
5281 diff = nowawareplus - delta
5282 self.assertIs(diff.tzinfo, tz55)
5283 self.assertEqual(nowaware, diff)
5284 self.assertRaises(TypeError, lambda: delta - nowawareplus)
5285 self.assertEqual(nowawareplus - nowaware, delta)
5286
5287 # Make up a random timezone.
5288 tzr = FixedOffset(random.randrange(-1439, 1440), "randomtimezone")
5289 # Attach it to nowawareplus.
5290 nowawareplus = nowawareplus.replace(tzinfo=tzr)
5291 self.assertIs(nowawareplus.tzinfo, tzr)
5292 # Make sure the difference takes the timezone adjustments into account.
5293 got = nowaware - nowawareplus
5294 # Expected: (nowaware base - nowaware offset) -
5295 # (nowawareplus base - nowawareplus offset) =
5296 # (nowaware base - nowawareplus base) +
5297 # (nowawareplus offset - nowaware offset) =
5298 # -delta + nowawareplus offset - nowaware offset
5299 expected = nowawareplus.utcoffset() - nowaware.utcoffset() - delta
5300 self.assertEqual(got, expected)
5301
5302 # Try max possible difference.
5303 min = self.theclass(1, 1, 1, tzinfo=FixedOffset(1439, "min"))
5304 max = self.theclass(MAXYEAR, 12, 31, 23, 59, 59, 999999,
5305 tzinfo=FixedOffset(-1439, "max"))
5306 maxdiff = max - min
5307 self.assertEqual(maxdiff, self.theclass.max - self.theclass.min +

Callers

nothing calls this directly

Calls 14

timedeltaClass · 0.90
timezoneClass · 0.90
FixedOffsetClass · 0.85
nowMethod · 0.80
dateMethod · 0.80
timetzMethod · 0.80
randrangeMethod · 0.80
replaceMethod · 0.45
timeMethod · 0.45
combineMethod · 0.45
assertIsMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected