fix(time): precisedelta keeps the sign of negative timedeltas - #380
Open
Mukller wants to merge 3 commits into
Open
fix(time): precisedelta keeps the sign of negative timedeltas#380Mukller wants to merge 3 commits into
Mukller wants to merge 3 commits into
Conversation
for more information, see https://pre-commit.ci
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.
Fixes #379
precisedelta()silently dropped the sign of negative timedeltas:-3661 srendered byte-identical to
+3661 s, making the function unsafe for signedinputs.
Root cause
The sign was lost before
precisedelta()even ran: the shared helper_date_and_delta()returns_abs_timedelta(delta)(time.py), so every callerreceives the absolute value.
precisedeltathen decomposed already-positivecomponents.
Changes
precisedelta()captures the sign from the original value (timedeltacomparison, datetime-vs-now comparison, or numeric
< 0) and prefixes therendered string with
-for negative inputs. Zero stays unsigned.Testing
Four new tests in
tests/test_time.py: negative multi-unit, negative with acoarser
minimum_unit(float path), zero-stays-unsigned, positive-unchanged.Full suite: 704 passed, 74 skipped.
Note:
naturaldelta()shares the same helper, so it presumably also drops thesign — left out of scope here to keep this PR focused, but worth a separate
issue if maintainers agree it's undesired there too.