Commit b0c854d6 authored by Tim Peters's avatar Tim Peters

datetime.timedelta is now subclassable in Python. The new test shows

one good use:  a subclass adding a method to express the duration as
a number of hours (or minutes, or whatever else you want to add).  The
native breakdown into days+seconds+us is often clumsy.  Incidentally
moved a large chunk of object-initialization code closer to the top of
the file, to avoid worse forward-reference trickery.
parent 108c40c7
......@@ -443,6 +443,37 @@ class TestTimeDelta(HarmlessMixedComparison):
self.failUnless(timedelta(microseconds=1))
self.failUnless(not timedelta(0))
def test_subclass_timedelta(self):
class T(timedelta):
def from_td(td):
return T(td.days, td.seconds, td.microseconds)
from_td = staticmethod(from_td)
def as_hours(self):
sum = (self.days * 24 +
self.seconds / 3600.0 +
self.microseconds / 3600e6)
return round(sum)
t1 = T(days=1)
self.assert_(type(t1) is T)
self.assertEqual(t1.as_hours(), 24)
t2 = T(days=-1, seconds=-3600)
self.assert_(type(t2) is T)
self.assertEqual(t2.as_hours(), -25)
t3 = t1 + t2
self.assert_(type(t3) is timedelta)
t4 = T.from_td(t3)
self.assert_(type(t4) is T)
self.assertEqual(t3.days, t4.days)
self.assertEqual(t3.seconds, t4.seconds)
self.assertEqual(t3.microseconds, t4.microseconds)
self.assertEqual(str(t3), str(t4))
self.assertEqual(t4.as_hours(), -1)
#############################################################################
# date tests
......
......@@ -26,8 +26,8 @@ Core and builtins
Extension modules
-----------------
- The datetime.datetime and datetime.time classes are now properly
subclassable.
- The datetime module classes datetime, time, and timedelta are now
properly subclassable.
- _tkinter.{get|set}busywaitinterval was added.
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment