Skip to content

Commit

Permalink
feat: ✨ add __rmul__ to relativedelta
Browse files Browse the repository at this point in the history
  • Loading branch information
olliemath committed Aug 18, 2023
1 parent 95bac5c commit d2a960f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/test_relativedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_duration_arithmetic():

assert x // 2 == relativedelta(months=5 * 6 + 3, seconds=50)
assert x * 2 == relativedelta(months=10 * 12 + 14, seconds=200)
assert 2 * x == x * 2, "Multiplication should be symmetric"

# Dividing a 1-month by e.g. 2 has no unambiguous meaning
with pytest.raises(TypeError, match="unsupported operand"):
Expand Down
3 changes: 3 additions & 0 deletions urelativedelta/relativedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ def __rsub__(self, other):
def __mul__(self, n: int) -> RelativeDelta:
return self.__class__(months=self.months * n, timedelta=self.timedelta * n)

def __rmul__(self, n: int) -> RelativeDelta:
return self * n

def __floordiv__(self, n: int) -> RelativeDelta:
return self.__class__(months=self.months // n, timedelta=self.timedelta // n)

Expand Down

0 comments on commit d2a960f

Please sign in to comment.