datetime: Avoid float.as_integer_ratio().

MicroPython doesn't have it, so implement equivalent (+/- rounding errors)
arithmetics.
pull/234/merge
Paul Sokolovsky 2017-12-18 00:39:01 +02:00
rodzic 1bde1058ce
commit fe2b6d473a
1 zmienionych plików z 7 dodań i 4 usunięć

Wyświetl plik

@ -505,8 +505,10 @@ class timedelta:
self._seconds * other, self._seconds * other,
self._microseconds * other) self._microseconds * other)
if isinstance(other, float): if isinstance(other, float):
a, b = other.as_integer_ratio() #a, b = other.as_integer_ratio()
return self * a / b #return self * a / b
usec = self._to_microseconds()
return timedelta(0, 0, round(usec * other))
return NotImplemented return NotImplemented
__rmul__ = __mul__ __rmul__ = __mul__
@ -533,8 +535,9 @@ class timedelta:
if isinstance(other, int): if isinstance(other, int):
return timedelta(0, 0, usec / other) return timedelta(0, 0, usec / other)
if isinstance(other, float): if isinstance(other, float):
a, b = other.as_integer_ratio() # a, b = other.as_integer_ratio()
return timedelta(0, 0, b * usec / a) # return timedelta(0, 0, b * usec / a)
return timedelta(0, 0, round(usec / other))
def __mod__(self, other): def __mod__(self, other):
if isinstance(other, timedelta): if isinstance(other, timedelta):