Skip to content

fix: preserve microsecond precision in Duration for large values#969

Open
SAY-5 wants to merge 1 commit into
python-pendulum:masterfrom
SAY-5:fix-duration-microsecond-precision
Open

fix: preserve microsecond precision in Duration for large values#969
SAY-5 wants to merge 1 commit into
python-pendulum:masterfrom
SAY-5:fix-duration-microsecond-precision

Conversation

@SAY-5
Copy link
Copy Markdown
Contributor

@SAY-5 SAY-5 commented May 24, 2026

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code.

Fixes #332.

Duration derived its normalized microseconds/seconds/days fields from total_seconds(), which returns a float and loses precision for large values:

>>> pendulum.duration(microseconds=8999999999999999).microseconds
999998  # datetime.timedelta returns 999999

The normalization now works on exact integer microseconds taken from the parent timedelta fields, matching datetime.timedelta behavior. The same float-precision issue in AbsoluteDuration (used by Time.diff()) is fixed the same way.

No documentation change needed; this restores the documented microseconds semantics.

Signed-off-by: SAY-5 <say.apm35@gmail.com>
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 24, 2026

Merging this PR will not alter performance

✅ 1 untouched benchmark


Comparing SAY-5:fix-duration-microsecond-precision (fa6a987) with master (b99bd14)

Open in CodSpeed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

duration loses microsecond precision on large values

1 participant