Skip to content

Commit 3bc22c5

Browse files
authored
Merge pull request #8 from adafruit/jepler-integer-total_seconds
total_seconds: Fix precision problem
2 parents c848678 + 13f9332 commit 3bc22c5

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)