Skip to content

Commit 637ca83

Browse files
author
Richard Jones
committed
implemented the missing Interval.__add__
1 parent f1b9441 commit 637ca83

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

roundup/date.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: date.py,v 1.34 2002-10-10 08:24:37 richard Exp $
18+
# $Id: date.py,v 1.35 2002-10-11 01:25:40 richard Exp $
1919

2020
__doc__ = """
2121
Date, time and time interval handling.
@@ -354,6 +354,22 @@ def __str__(self):
354354
l.append('%d:%02d'%(self.hour, self.minute))
355355
return ' '.join(l)
356356

357+
def __add__(self, other):
358+
if isinstance(other, Date):
359+
# the other is a Date - produce a Date
360+
return Date(other.addInterval(self))
361+
elif isinstance(other, Interval):
362+
# add the other Interval to this one
363+
a = self.get_tuple()
364+
b = other.get_tuple()
365+
if b[0] < 0:
366+
i = Interval([x-y for x,y in zip(a[1:],b[1:])])
367+
else:
368+
i = Interval([x+y for x,y in zip(a[1:],b[1:])])
369+
return i
370+
# nope, no idea what to do with this other...
371+
raise TypeError, "Can't add %r"%other
372+
357373
def set(self, spec, interval_re=re.compile('''
358374
\s*(?P<s>[-+])? # + or -
359375
\s*((?P<y>\d+\s*)y)? # year

test/test_dates.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: test_dates.py,v 1.13 2002-09-10 00:19:54 richard Exp $
18+
# $Id: test_dates.py,v 1.14 2002-10-11 01:25:40 richard Exp $
1919

2020
import unittest, time
2121

@@ -148,6 +148,15 @@ def testInterval(self):
148148
ae(str(Interval(' 14:00 ')), '+ 14:00')
149149
ae(str(Interval(' 0:04:33 ')), '+ 0:04:33')
150150

151+
# __add__
152+
# XXX these are fairly arbitrary and need fixing once the __add__
153+
# code handles the odd cases more correctly
154+
ae(str(Interval('1y') + Interval('1y')), '+ 2y')
155+
ae(str(Interval('1y') + Interval('1m')), '+ 1y 1m')
156+
ae(str(Interval('1y') + Interval('2:40')), '+ 1y 2:40')
157+
ae(str(Interval('1y') + Interval('- 1y')), '+')
158+
ae(str(Interval('1y') + Interval('- 1m')), '+ 1y -1m')
159+
151160
def suite():
152161
return unittest.makeSuite(DateTestCase, 'test')
153162

0 commit comments

Comments
 (0)