[Python-checkins] python/nondist/sandbox/datetime test_both.py,1.80,1.81
tim_one@users.sourceforge.net
tim_one@users.sourceforge.net
Sat, 14 Dec 2002 01:01:27 -0800
Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv27857
Modified Files:
test_both.py
Log Message:
Added test of tzinfo-aware datetimetz arithmetic.
Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.80
retrieving revision 1.81
diff -C2 -d -r1.80 -r1.81
*** test_both.py 14 Dec 2002 08:03:14 -0000 1.80
--- test_both.py 14 Dec 2002 09:01:24 -0000 1.81
***************
*** 1864,1867 ****
--- 1864,1935 ----
self.assertEqual(dt.timetz(), timetz(18, 45, 3, 1234, tzinfo=met))
+ def test_tz_aware_arithmetic(self):
+ import random
+
+ now = self.theclass.now()
+ tz55 = FixedOffset(-330, "west 5:30")
+ timeaware = timetz(now.hour, now.minute, now.second,
+ now.microsecond, tzinfo=tz55)
+ nowaware = self.theclass.combine(now.date(), timeaware)
+ self.failUnless(nowaware.tzinfo is tz55)
+ self.assertEqual(nowaware.timetz(), timeaware)
+
+ # Can't mix aware and non-aware.
+ self.assertRaises(TypeError, lambda: now - nowaware)
+ self.assertRaises(TypeError, lambda: nowaware - now)
+
+ # And adding datetimetz's doesn't make sense, aware or not.
+ self.assertRaises(TypeError, lambda: now + nowaware)
+ self.assertRaises(TypeError, lambda: nowaware + now)
+ self.assertRaises(TypeError, lambda: nowaware + nowaware)
+
+ # Subtracting should yield 0.
+ self.assertEqual(now - now, timedelta(0))
+ self.assertEqual(nowaware - nowaware, timedelta(0))
+
+ # Adding a delta should preserve tzinfo.
+ delta = timedelta(weeks=1, minutes=12, microseconds=5678)
+ nowawareplus = nowaware + delta
+ self.failUnless(nowaware.tzinfo is tz55)
+ nowawareplus2 = delta + nowaware
+ self.failUnless(nowawareplus2.tzinfo is tz55)
+ self.assertEqual(nowawareplus, nowawareplus2)
+
+ # that - delta should be what we started with, and that - what we
+ # started with should be delta.
+ diff = nowawareplus - delta
+ self.failUnless(diff.tzinfo is tz55)
+ self.assertEqual(nowaware, diff)
+ self.assertRaises(TypeError, lambda: delta - nowawareplus)
+ self.assertEqual(nowawareplus - nowaware, delta)
+
+ # Make up a random timezone.
+ tzr = FixedOffset(random.randrange(-1439, 1440), "randomtimezone")
+ # Attach it to nowawareplus -- this is clumsy.
+ nowawareplus = self.theclass.combine(nowawareplus.date(),
+ timetz(nowawareplus.hour,
+ nowawareplus.minute,
+ nowawareplus.second,
+ nowawareplus.microsecond,
+ tzinfo=tzr))
+ self.failUnless(nowawareplus.tzinfo is tzr)
+ # Make sure the difference takes the timezone adjustments into account.
+ got = nowaware - nowawareplus
+ # Expected: (nowaware base - nowaware offset) -
+ # (nowawareplus base - nowawareplus offset) =
+ # (nowaware base - nowawareplus base) +
+ # (nowawareplus offset - nowaware offset) =
+ # -delta + nowawareplus offset - nowaware offset
+ expected = timedelta(minutes=nowawareplus.utcoffset() -
+ nowaware.utcoffset()) - delta
+ self.assertEqual(got, expected)
+
+ # Try max possible difference.
+ min = self.theclass(1, 1, 1, tzinfo=FixedOffset(1439, "min"))
+ max = self.theclass(MAXYEAR, 12, 31, 23, 59, 59, 999999,
+ tzinfo=FixedOffset(-1439, "max"))
+ maxdiff = max - min
+ self.assertEqual(maxdiff, self.theclass.max - self.theclass.min +
+ timedelta(minutes=2*1439))
def test_suite():