fix: correct SecondsTimedeltaProvider for negative fractional seconds - #453
Merged
zhPavel merged 2 commits intoJul 12, 2026
Merged
Conversation
`timedelta(seconds=int(data), microseconds=int(data % 1 * 10**6))` uses
`int()` which truncates toward zero, so for `data = -0.5` it computes
`seconds=0, microseconds=500000` (+0.5 s) instead of the correct -0.5 s.
Replace with `math.floor()` to get the proper floor-division semantics:
`sec = math.floor(data)` and `microseconds=round((data - sec) * 10**6)`.
The `round()` avoids off-by-one microsecond errors from float arithmetic
(e.g. `-2.7 % 1 * 1e6 → 299999.9...`). Decimal inputs are handled
correctly too since `math.floor(Decimal(...))` returns an int.
Reproducer:
retort.load(-0.5, timedelta) # returned timedelta(0, 0, 500000) (+0.5 s)
retort.load(-1.5, timedelta) # returned timedelta(-1, 86399, 500000) (-0.5 s)
|
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Member
|
Thank you for your contribution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Bug
SecondsTimedeltaProviderreturns the wrongtimedeltawhen loading anegative fractional number of seconds.
Root cause
The loader uses:
int()truncates toward zero, so fordata = -0.5:int(-0.5)→0(should be-1)-0.5 % 1→0.5(Python modulo is always non-negative)timedelta(seconds=0, microseconds=500000)→ +0.5 s ❌Fix
Use
math.floor()for the seconds component, which gives floor-divisionsemantics (always rounds toward negative infinity):
round()is used for the microseconds to avoid off-by-one float-precisionerrors (e.g.
-2.7 % 1 * 1e6 → 299999.99...).Decimalinputs arehandled correctly too since
math.floor(Decimal(...))returns anint.Tests
Added four assertions for negative fractional
floatandDecimalinputsto
test_seconds_timedelta_provider. All 6 parametrized variants pass.This pull request was prepared with the assistance of AI, under my direction and review.