Skip to content

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
@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

Development

Successfully merging this pull request may close these issues.

Negative amounts of time result in a day from naturaldelta naturaldelta with negative seconds loses precision and doesn't match naturaltime

1 participant