Skip to content
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

Fix handling of negative time deltas #173

Merged
merged 5 commits into from
Feb 25, 2024

Conversation

hugovk
Copy link
Member

@hugovk hugovk commented Feb 24, 2024

Fixes #18.
Fixes #171.

The Python docs say:

String representations of timedelta objects are normalized similarly to their internal representation. This leads to somewhat unusual results for negative timedeltas. For example:

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00

However, we assumed all components have the same sign, and used absolute values of some of those, leading to inconsistent behaviour (on Linux and macOS; it still worked on Windows).

Instead, we should convert the whole timedelta into its absolute value, and then use its components directly.

Also test all the timedeltas with positive and negative values.

Because a small delta can be represented as timedelta(days=-1, seconds=86399, microseconds=999999), so we should assume all components have the same sign
@hugovk hugovk added bug Something isn't working changelog: Fixed For any bug fixes labels Feb 24, 2024
Copy link

codecov bot commented Feb 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.19%. Comparing base (584d470) to head (ec99f29).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #173   +/-   ##
=======================================
  Coverage   99.19%   99.19%           
=======================================
  Files           9        9           
  Lines         746      747    +1     
=======================================
+ Hits          740      741    +1     
  Misses          6        6           
Flag Coverage Δ
macos-latest 97.59% <100.00%> (+<0.01%) ⬆️
ubuntu-latest 97.59% <100.00%> (+<0.01%) ⬆️
windows-latest 96.11% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hugovk hugovk merged commit 218a86e into python-humanize:main Feb 25, 2024
32 checks passed
@hugovk hugovk deleted the fix-small-timedelta branch February 25, 2024 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working changelog: Fixed For any bug fixes
Projects
None yet
1 participant