Skip to content

Commit

Permalink
Implement mod and divmod for durations
Browse files Browse the repository at this point in the history
And fix them to return integers not floats for floor division, to match `datetime.timedelta`
  • Loading branch information
eric-wieser authored and dirk-thomas committed Jun 24, 2016
1 parent f2e20be commit 803b5ba
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/genpy/rostime.py
Expand Up @@ -368,7 +368,7 @@ def __floordiv__(self, val):
if isinstance(val, numbers.Integral) or isinstance(val, numbers.Real):
return Duration.from_sec(self.to_sec() // val)
elif isinstance(val, Duration):
return self.to_sec() // val.to_sec()
return int(self.to_sec() // val.to_sec())
else:
return NotImplemented

Expand Down Expand Up @@ -398,6 +398,28 @@ def __truediv__(self, val):
else:
return NotImplemented

def __mod__(self, val):
"""
Find the remainder when dividing this Duration by another Duration
:returns: :class:`Duration` The remaining time after the division
"""
if isinstance(val, Duration):
return Duration.from_sec(self.to_sec() % val.to_sec())
else:
return NotImplemented

def __divmod__(self, val):
"""
Implements the builtin divmod for a pair of Durations
:returns: ``int`` The floored result of the division
:returns: :class:`Duration` The remaining time after the division
"""
if isinstance(val, Duration):
quotient, remainder = divmod(self.to_sec(), val.to_sec())
return int(quotient), Duration.from_sec(remainder)
else:
return NotImplemented

def __cmp__(self, other):
if not isinstance(other, Duration):
raise TypeError("Cannot compare to non-Duration")
Expand Down

0 comments on commit 803b5ba

Please sign in to comment.