Skip to content

Commit

Permalink
Merge pull request #49 from dateutil/relativedelta-enhancements
Browse files Browse the repository at this point in the history
Relativedelta enhancements - inheritance and weeks property.
  • Loading branch information
pganssle committed Mar 7, 2015
2 parents 5a611fb + 42248dc commit acb07a2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
17 changes: 12 additions & 5 deletions dateutil/relativedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def __init__(self, dt1=None, dt2=None,
else:
self.years = years
self.months = months
self.days = days+weeks*7
self.days = days + weeks * 7
self.leapdays = leapdays
self.hours = hours
self.minutes = minutes
Expand Down Expand Up @@ -242,6 +242,13 @@ def _fix(self):
else:
self._has_time = 0

@property
def weeks(self):
return self.days // 7
@weeks.setter
def weeks(self, value):
self.days = self.days - (self.weeks * 7) + value*7

def _set_months(self, months):
self.months = months
if abs(self.months) > 11:
Expand All @@ -254,7 +261,7 @@ def _set_months(self, months):

def __add__(self, other):
if isinstance(other, relativedelta):
return relativedelta(years=other.years+self.years,
return self.__class__(years=other.years+self.years,
months=other.months+self.months,
days=other.days+self.days,
hours=other.hours+self.hours,
Expand Down Expand Up @@ -323,7 +330,7 @@ def __rsub__(self, other):
def __sub__(self, other):
if not isinstance(other, relativedelta):
raise TypeError("unsupported type for sub operation")
return relativedelta(years=self.years-other.years,
return self.__class__(years=self.years-other.years,
months=self.months-other.months,
days=self.days-other.days,
hours=self.hours-other.hours,
Expand All @@ -341,7 +348,7 @@ def __sub__(self, other):
microsecond=self.microsecond or other.microsecond)

def __neg__(self):
return relativedelta(years=-self.years,
return self.__class__(years=-self.years,
months=-self.months,
days=-self.days,
hours=-self.hours,
Expand Down Expand Up @@ -380,7 +387,7 @@ def __bool__(self):

def __mul__(self, other):
f = float(other)
return relativedelta(years=int(self.years*f),
return self.__class__(years=int(self.years*f),
months=int(self.months*f),
days=int(self.days*f),
hours=int(self.hours*f),
Expand Down
35 changes: 35 additions & 0 deletions dateutil/test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@ class RelativeDeltaTest(unittest.TestCase):
now = datetime(2003, 9, 17, 20, 54, 47, 282310)
today = date(2003, 9, 17)

def testInheritance(self):
# Ensure that relativedelta is inheritance-friendly.
class rdChildClass(relativedelta):
pass

ccRD = rdChildClass(years=1, months=1, days=1, leapdays=1, weeks=1,
hours=1, minutes=1, seconds=1, microseconds=1)

rd = relativedelta(years=1, months=1, days=1, leapdays=1, weeks=1,
hours=1, minutes=1, seconds=1, microseconds=1)

self.assertEqual(type(ccRD + rd), type(ccRD),
msg='Addition does not inherit type.')

self.assertEqual(type(ccRD - rd), type(ccRD),
msg='Subtraction does not inherit type.')

self.assertEqual(type(-ccRD), type(ccRD),
msg='Negation does not inherit type.')

self.assertEqual(type(ccRD * 5.0), type(ccRD),
msg='Multiplication does not inherit type.')

self.assertEqual(type(ccRD / 5.0), type(ccRD),
msg='Division does not inherit type.')


def testNextMonth(self):
self.assertEqual(self.now+relativedelta(months=+1),
datetime(2003, 10, 17, 20, 54, 47, 282310))
Expand Down Expand Up @@ -197,6 +224,14 @@ def testBoolean(self):
self.assertFalse(relativedelta(days=0))
self.assertTrue(relativedelta(days=1))

def testWeeks(self):
# Test that the weeks property is working properly.
rd = relativedelta(years=4, months=2, weeks=8, days=6)
self.assertEqual((rd.weeks, rd.days), (8, 8 * 7 + 6))

rd.weeks = 3
self.assertEqual((rd.weeks, rd.days), (3, 3 * 7 + 6))


class RRuleTest(unittest.TestCase):
def testYearly(self):
Expand Down

0 comments on commit acb07a2

Please sign in to comment.