Skip to content

Fix precisedelta to handle negative timedelta - #412

Closed
agbesiprincekwabena-cmd wants to merge 2 commits into
python-humanize:mainfrom
agbesiprincekwabena-cmd:fix-379-precisedelta-negative
Closed

agbesiprincekwabena-cmd wants to merge 2 commits into
python-humanize:mainfrom
agbesiprincekwabena-cmd:fix-379-precisedelta-negative

Conversation

@agbesiprincekwabena-cmd

Copy link
Copy Markdown

Fixes #

Changes proposed in this pull request:

Fixes #379

Problem

precisedelta() returned empty string or incorrect values for negative timedelta (e.g. -1 day showed as "1 day" or "" instead of "1 day ago").

The abs() logic in time.py was incorrectly converting negative deltas.

Solution

  • Handle negative timedelta by tracking sign separately
  • Convert to positive for calculation, then append " ago" suffix if original was negative
  • Preserves existing behavior for positive deltas

Tested with:

  • timedelta(days=-1) -> "1 day ago"
  • timedelta(days=-1, hours=-2) -> "1 day, 2 hours ago"
  • Positive deltas still work as before

@hugovk

hugovk commented Sep 24, 2026

Copy link
Copy Markdown
Member

Closing as a duplicate of #380 which you would see from looking at #379.

Please check next time.

@hugovk hugovk closed this Sep 24, 2026
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.

precisedelta() silently drops the sign of negative timedeltas

2 participants