Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement mod and divmod for durations #61

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/genpy/rostime.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ def __floordiv__(self, val):
elif t == float:
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 @@ -446,6 +446,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