diff options
author | Tomi Pieviläinen <tomi.pievilainen+launchpad@iki.fi> | 2012-03-28 11:36:48 +0300 |
---|---|---|
committer | Tomi Pieviläinen <tomi.pievilainen+launchpad@iki.fi> | 2012-03-28 11:36:48 +0300 |
commit | 15fc1fa8c1e2c3d088e646d6d47ac9d25d902f95 (patch) | |
tree | 380159ffcc69cbb55c71cee5e4f4a80a4cbff963 | |
parent | 836f16166a473e379d59478e7e26b1cd5a61bcf0 (diff) | |
download | dateutil-15fc1fa8c1e2c3d088e646d6d47ac9d25d902f95.tar.gz |
Fix relativedelta arithmetics, bug #965881
-rw-r--r-- | dateutil/relativedelta.py | 74 | ||||
-rwxr-xr-x | test.py | 37 |
2 files changed, 66 insertions, 45 deletions
diff --git a/dateutil/relativedelta.py b/dateutil/relativedelta.py index 67de55e..5d09731 100644 --- a/dateutil/relativedelta.py +++ b/dateutil/relativedelta.py @@ -243,7 +243,24 @@ Here is the behavior of operations with relativedelta: else: self.years = 0 - def __radd__(self, other): + def __add__(self, other): + if isinstance(other, relativedelta): + return relativedelta(years=other.years+self.years, + months=other.months+self.months, + days=other.days+self.days, + hours=other.hours+self.hours, + minutes=other.minutes+self.minutes, + seconds=other.seconds+self.seconds, + microseconds=other.microseconds+self.microseconds, + leapdays=other.leapdays or self.leapdays, + year=other.year or self.year, + month=other.month or self.month, + day=other.day or self.day, + weekday=other.weekday or self.weekday, + hour=other.hour or self.hour, + minute=other.minute or self.minute, + second=other.second or self.second, + microsecond=other.microsecond or self.microsecond) if not isinstance(other, datetime.date): raise TypeError("unsupported type for add operation") elif self._has_time and not isinstance(other, datetime.datetime): @@ -286,48 +303,31 @@ Here is the behavior of operations with relativedelta: ret += datetime.timedelta(days=jumpdays) return ret + def __radd__(self, other): + return self.__add__(other) + def __rsub__(self, other): return self.__neg__().__radd__(other) - def __add__(self, other): - if not isinstance(other, relativedelta): - raise TypeError("unsupported type for add operation") - return relativedelta(years=other.years+self.years, - months=other.months+self.months, - days=other.days+self.days, - hours=other.hours+self.hours, - minutes=other.minutes+self.minutes, - seconds=other.seconds+self.seconds, - microseconds=other.microseconds+self.microseconds, - leapdays=other.leapdays or self.leapdays, - year=other.year or self.year, - month=other.month or self.month, - day=other.day or self.day, - weekday=other.weekday or self.weekday, - hour=other.hour or self.hour, - minute=other.minute or self.minute, - second=other.second or self.second, - microsecond=other.microsecond or self.microsecond) - def __sub__(self, other): if not isinstance(other, relativedelta): raise TypeError("unsupported type for sub operation") - return relativedelta(years=other.years-self.years, - months=other.months-self.months, - days=other.days-self.days, - hours=other.hours-self.hours, - minutes=other.minutes-self.minutes, - seconds=other.seconds-self.seconds, - microseconds=other.microseconds-self.microseconds, - leapdays=other.leapdays or self.leapdays, - year=other.year or self.year, - month=other.month or self.month, - day=other.day or self.day, - weekday=other.weekday or self.weekday, - hour=other.hour or self.hour, - minute=other.minute or self.minute, - second=other.second or self.second, - microsecond=other.microsecond or self.microsecond) + return relativedelta(years=self.years-other.years, + months=self.months-other.months, + days=self.days-other.days, + hours=self.hours-other.hours, + minutes=self.minutes-other.minutes, + seconds=self.seconds-other.seconds, + microseconds=self.microseconds-other.microseconds, + leapdays=self.leapdays or other.leapdays, + year=self.year or other.year, + month=self.month or other.month, + day=self.day or other.day, + weekday=self.weekday or other.weekday, + hour=self.hour or other.hour, + minute=self.minute or other.minute, + second=self.second or other.second, + microsecond=self.microsecond or other.microsecond) def __neg__(self): return relativedelta(years=-self.years, @@ -150,14 +150,35 @@ class RelativeDeltaTest(unittest.TestCase): self.assertEqual(self.today+relativedelta(yearday=261), date(2003, 9, 18)) - def test884317(self): - # See https://bugs.launchpad.net/dateutil/+bug/884317 - a = relativedelta(second = 2, microsecond = 20) - b = relativedelta(second = 1, microsecond = 10) - c = a-b - self.assertEqual(c.microsecond, b.microsecond) - c = a+b - self.assertEqual(c.microsecond, b.microsecond) + def testAddition(self): + self.assertEqual(relativedelta(days=10) + + relativedelta(years=1, months=2, days=3, hours=4, + minutes=5, microseconds=6), + relativedelta(years=1, months=2, days=13, hours=4, + minutes=5, microseconds=6)) + + def testAdditionToDatetime(self): + self.assertEqual(datetime(2000, 1, 1) + relativedelta(days=1), + datetime(2000, 1, 2)) + + def testRightAdditionToDatetime(self): + self.assertEqual(relativedelta(days=1) + datetime(2000, 1, 1), + datetime(2000, 1, 2)) + + def testSubtraction(self): + self.assertEqual(relativedelta(days=10) - + relativedelta(years=1, months=2, days=3, hours=4, + minutes=5, microseconds=6), + relativedelta(years=-1, months=-2, days=7, hours=-4, + minutes=-5, microseconds=-6)) + + def testRightSubtractionFromDatetime(self): + self.assertEqual(datetime(2000, 1, 2) - relativedelta(days=1), + datetime(2000, 1, 1)) + + def testSubractionWithDatetime(self): + self.assertRaises(TypeError, lambda x, y: x - y, + (relativedelta(days=1), datetime(2000, 1, 1))) def testMultiplication(self): self.assertEqual(datetime(2000, 1, 1) + relativedelta(days=1) * 28, |