Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(85262)

Unified Diff: Lib/test/datetimetester.py

Issue 17267: datetime.time support for '+' and 'now'
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
index 931ef6f..55d199e 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -2041,6 +2041,27 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase):
self.assertEqual(t.second, 0)
self.assertEqual(t.microsecond, 0)
+ def test_computations(self):
+ a = self.theclass(0, 0, 0)
+ b = self.theclass(23, 59)
+
+ minute = timedelta(minutes=1)
+ hour = timedelta(hours=1)
+ week = timedelta(days=7)
+ self.assertEqual(a + minute, self.theclass(0, 1, 0))
+ self.assertEqual(b + minute + minute, self.theclass(0, 1))
+ self.assertEqual(b + minute, self.theclass(0, 0))
+ self.assertEqual(a + week, self.theclass(0))
+ self.assertEqual(a + week, self.theclass(0))
+ self.assertEqual(b + hour * 12, self.theclass(11, 59))
+
+ # Subtracting
+ self.assertEqual(a - minute, self.theclass(23, 59))
+ self.assertEqual(a - minute * 2, self.theclass(23, 58))
+ self.assertEqual(b - hour * 24, self.theclass(23, 59))
+ self.assertEqual(b - week, self.theclass(23, 59))
+ self.assertEqual(b - week - hour, self.theclass(22, 59))
+
def test_basic_attributes_nonzero(self):
# Make sure all attributes are non-zero so bugs in
# bit-shifting access show up.
@@ -2330,6 +2351,7 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase):
self.assertRaises(TypeError, self.theclass,
hour_byte + base[1:])
+
# A mixin for classes with a tzinfo= argument. Subclasses must define
# theclass as a class atribute, and theclass(1, 1, 1, tzinfo=whatever)
# must be legit (which is true for time and datetime).
@@ -2497,6 +2519,46 @@ class TZInfoBase:
class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase):
theclass = time
+ def test_computations(self):
+ est = FixedOffset(-300, "EST", 1)
+ utc = FixedOffset(0, "UTC", -2)
+ met = FixedOffset(60, "MET", 3)
+
+ minute = timedelta(minutes=1)
+ hour = timedelta(hours=1)
+ week = timedelta(days=7)
+
+ for tz in [est, utc, met]:
+ a = self.theclass(0, 0, 0, tzinfo=tz)
+ b = self.theclass(23, 59, tzinfo=tz)
+ self.assertEqual(a + minute, self.theclass(0, 1, 0, tzinfo=tz))
+ self.assertEqual(b + minute + minute, self.theclass(0, 1, tzinfo=tz))
+ self.assertEqual(b + minute, self.theclass(0, 0, tzinfo=tz))
+ self.assertEqual(a + week, self.theclass(0, tzinfo=tz))
+ self.assertEqual(a + week, self.theclass(0, tzinfo=tz))
+ self.assertEqual(b + hour * 12, self.theclass(11, 59, tzinfo=tz))
+
+ c = self.theclass(0, 0, 0, tzinfo=est)
+ d = self.theclass(23, 59, 0, tzinfo=met)
+
+ self.assertEqual(c + hour * 5, self.theclass(10, 0, tzinfo=utc))
+ self.assertEqual(d + hour + minute, self.theclass(0, 0, tzinfo=utc))
+
+ # Subtracting
+ for tz in [est, utc, met]:
+ a = self.theclass(0, 0, 0, tzinfo=tz)
+ b = self.theclass(23, 59, tzinfo=tz)
+
+ self.assertEqual(a - minute, self.theclass(23, 59, tzinfo=tz))
+ self.assertEqual(a - minute * 2, self.theclass(23, 58, tzinfo=tz))
+ self.assertEqual(b - hour * 24, self.theclass(23, 59, tzinfo=tz))
+ self.assertEqual(b - week, self.theclass(23, 59, tzinfo=tz))
+ self.assertEqual(b - week - hour, self.theclass(22, 59, tzinfo=tz))
+
+ e = self.theclass(0, tzinfo=est)
+
+ self.assertEqual(e - hour * 5, self.theclass(0, tzinfo=utc))
+
def test_empty(self):
t = self.theclass()
self.assertEqual(t.hour, 0)
« Lib/datetime.py ('K') | « Lib/datetime.py ('k') | Modules/_datetimemodule.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+