From bf2b72da5fc968729ef6ab837d73c77a16c5904d Mon Sep 17 00:00:00 2001 From: richard Date: Fri, 11 Oct 2002 01:25:40 +0000 Subject: [PATCH] implemented the missing Interval.__add__ git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/trunk@1341 57a73879-2fb5-44c3-a270-3262357dd7e2 --- roundup/date.py | 18 +++++++++++++++++- test/test_dates.py | 11 ++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/roundup/date.py b/roundup/date.py index d074064..6299318 100644 --- a/roundup/date.py +++ b/roundup/date.py @@ -15,7 +15,7 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -# $Id: date.py,v 1.34 2002-10-10 08:24:37 richard Exp $ +# $Id: date.py,v 1.35 2002-10-11 01:25:40 richard Exp $ __doc__ = """ Date, time and time interval handling. @@ -354,6 +354,22 @@ class Interval: l.append('%d:%02d'%(self.hour, self.minute)) return ' '.join(l) + def __add__(self, other): + if isinstance(other, Date): + # the other is a Date - produce a Date + return Date(other.addInterval(self)) + elif isinstance(other, Interval): + # add the other Interval to this one + a = self.get_tuple() + b = other.get_tuple() + if b[0] < 0: + i = Interval([x-y for x,y in zip(a[1:],b[1:])]) + else: + i = Interval([x+y for x,y in zip(a[1:],b[1:])]) + return i + # nope, no idea what to do with this other... + raise TypeError, "Can't add %r"%other + def set(self, spec, interval_re=re.compile(''' \s*(?P[-+])? # + or - \s*((?P\d+\s*)y)? # year diff --git a/test/test_dates.py b/test/test_dates.py index 4cbfd70..88accdb 100644 --- a/test/test_dates.py +++ b/test/test_dates.py @@ -15,7 +15,7 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -# $Id: test_dates.py,v 1.13 2002-09-10 00:19:54 richard Exp $ +# $Id: test_dates.py,v 1.14 2002-10-11 01:25:40 richard Exp $ import unittest, time @@ -148,6 +148,15 @@ class DateTestCase(unittest.TestCase): ae(str(Interval(' 14:00 ')), '+ 14:00') ae(str(Interval(' 0:04:33 ')), '+ 0:04:33') + # __add__ + # XXX these are fairly arbitrary and need fixing once the __add__ + # code handles the odd cases more correctly + ae(str(Interval('1y') + Interval('1y')), '+ 2y') + ae(str(Interval('1y') + Interval('1m')), '+ 1y 1m') + ae(str(Interval('1y') + Interval('2:40')), '+ 1y 2:40') + ae(str(Interval('1y') + Interval('- 1y')), '+') + ae(str(Interval('1y') + Interval('- 1m')), '+ 1y -1m') + def suite(): return unittest.makeSuite(DateTestCase, 'test') -- 2.30.2