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

Method test_tz_aware_arithmetic

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

Source from the content-addressed store, hash-verified

5061 self.assertEqual(dt.timetz(), time(18, 45, 3, 1234, tzinfo=met))
5062
5063 def test_tz_aware_arithmetic(self):
5064 now = self.theclass.now()
5065 tz55 = FixedOffset(-330, "west 5:30")
5066 timeaware = now.time().replace(tzinfo=tz55)
5067 nowaware = self.theclass.combine(now.date(), timeaware)
5068 self.assertIs(nowaware.tzinfo, tz55)
5069 self.assertEqual(nowaware.timetz(), timeaware)
5070
5071 # Can't mix aware and non-aware.
5072 self.assertRaises(TypeError, lambda: now - nowaware)
5073 self.assertRaises(TypeError, lambda: nowaware - now)
5074
5075 # And adding datetime's doesn't make sense, aware or not.
5076 self.assertRaises(TypeError, lambda: now + nowaware)
5077 self.assertRaises(TypeError, lambda: nowaware + now)
5078 self.assertRaises(TypeError, lambda: nowaware + nowaware)
5079
5080 # Subtracting should yield 0.
5081 self.assertEqual(now - now, timedelta(0))
5082 self.assertEqual(nowaware - nowaware, timedelta(0))
5083
5084 # Adding a delta should preserve tzinfo.
5085 delta = timedelta(weeks=1, minutes=12, microseconds=5678)
5086 nowawareplus = nowaware + delta
5087 self.assertIs(nowaware.tzinfo, tz55)
5088 nowawareplus2 = delta + nowaware
5089 self.assertIs(nowawareplus2.tzinfo, tz55)
5090 self.assertEqual(nowawareplus, nowawareplus2)
5091
5092 # that - delta should be what we started with, and that - what we
5093 # started with should be delta.
5094 diff = nowawareplus - delta
5095 self.assertIs(diff.tzinfo, tz55)
5096 self.assertEqual(nowaware, diff)
5097 self.assertRaises(TypeError, lambda: delta - nowawareplus)
5098 self.assertEqual(nowawareplus - nowaware, delta)
5099
5100 # Make up a random timezone.
5101 tzr = FixedOffset(random.randrange(-1439, 1440), "randomtimezone")
5102 # Attach it to nowawareplus.
5103 nowawareplus = nowawareplus.replace(tzinfo=tzr)
5104 self.assertIs(nowawareplus.tzinfo, tzr)
5105 # Make sure the difference takes the timezone adjustments into account.
5106 got = nowaware - nowawareplus
5107 # Expected: (nowaware base - nowaware offset) -
5108 # (nowawareplus base - nowawareplus offset) =
5109 # (nowaware base - nowawareplus base) +
5110 # (nowawareplus offset - nowaware offset) =
5111 # -delta + nowawareplus offset - nowaware offset
5112 expected = nowawareplus.utcoffset() - nowaware.utcoffset() - delta
5113 self.assertEqual(got, expected)
5114
5115 # Try max possible difference.
5116 min = self.theclass(1, 1, 1, tzinfo=FixedOffset(1439, "min"))
5117 max = self.theclass(MAXYEAR, 12, 31, 23, 59, 59, 999999,
5118 tzinfo=FixedOffset(-1439, "max"))
5119 maxdiff = max - min
5120 self.assertEqual(maxdiff, self.theclass.max - self.theclass.min +

Callers

nothing calls this directly

Calls 14

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

Tested by

no test coverage detected