Skip to content

Commit b1f9f0a

Browse files
committed
total_seconds: Fix precision problem
Since CircuitPython floats have very small precision, the "total_seconds" method was not very useful for large timedeltas. Instead of always returning a float, use pure-integer arithmetic (and return an int) if either: * the length of time is big (1<<21 is 2097152 seconds or about 24 days) * the number of microseconds is zero Otherwise, for small values with nonzero microseconds, use floating-point math. The cut-off point was chosen because in CircuitPython float arithmetic, 2097151.0+0.5 is different from 2097151.0, but 2097152.0+0.5 and 2097152.0 are *the same float*.
1 parent c0faca0 commit b1f9f0a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

adafruit_datetime.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,15 @@ def microseconds(self):
429429
# Instance methods
430430
def total_seconds(self):
431431
"""Return the total number of seconds contained in the duration."""
432-
return (
433-
(self._days * 86400 + self._seconds) * 10 ** 6 + self._microseconds
434-
) / 10 ** 6
432+
# If the duration is less than a threshold duration, and microseconds
433+
# is nonzero, then the result is a float. Otherwise, the result is a
434+
# (possibly long) integer. This differs from standard Python where the
435+
# result is always a float, because the precision of CircuitPython
436+
# floats is considerably smaller than on standard Python.
437+
seconds = self._days * 86400 + self._seconds
438+
if self._microseconds != 0 and abs(seconds) < (1 << 21):
439+
seconds += self._microseconds / 10 ** 6
440+
return seconds
435441

436442
def __repr__(self):
437443
args = []

0 commit comments

Comments
 (0)