Skip to content

total_seconds: Fix precision problem #8

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

Merged
merged 1 commit into from
Mar 26, 2021
Merged
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
12 changes: 9 additions & 3 deletions adafruit_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,15 @@ def microseconds(self):
# Instance methods
def total_seconds(self):
"""Return the total number of seconds contained in the duration."""
return (
(self._days * 86400 + self._seconds) * 10 ** 6 + self._microseconds
) / 10 ** 6
# If the duration is less than a threshold duration, and microseconds
# is nonzero, then the result is a float. Otherwise, the result is a
# (possibly long) integer. This differs from standard Python where the
# result is always a float, because the precision of CircuitPython
# floats is considerably smaller than on standard Python.
seconds = self._days * 86400 + self._seconds
if self._microseconds != 0 and abs(seconds) < (1 << 21):
seconds += self._microseconds / 10 ** 6
return seconds

def __repr__(self):
args = []
Expand Down