Skip to content

Fix OverflowError in naturaltime/precisedelta for far-future dates - #479

Open
tamsk-91 wants to merge 3 commits into
python-humanize:mainfrom
tamsk-91:main
Open

tamsk-91 wants to merge 3 commits into
python-humanize:mainfrom
tamsk-91:main

Conversation

@tamsk-91

Copy link
Copy Markdown

Fixes an OverflowError in naturaltime() and precisedelta() for far-future dates such as datetime(9999, 12, 31) by using abs(delta) in _abs_timedelta(). Adds regression tests.

@Muguett-DBY Muguett-DBY left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked 0a607ed with the clock fixed at 2010-02-02. The three added cases produce the expected strings with this change; the previous _abs_timedelta raises OverflowError for all three. For 505 smaller positive and negative durations, both naturaltime and precisedelta matched the previous implementation (1,010 comparisons).

One existing limit is separate from the helper this changes: with the same clock, precisedelta(timedelta(days=-3_000_000)) still raises OverflowError on both the base and this branch. _date_and_delta first computes now - value, which exceeds the maximum datetime, before _abs_timedelta is called. The same limit applies to naturaltime. This isn't a regression in this PR; it may be useful to keep the description scoped to the cases fixed here or track that boundary separately.

I ran the checked-out project source on Windows / Python 3.12 with the clock mocked and generated version metadata supplied in memory. I did not run pytest, freezegun, tox or the full suite. Review and validation assisted by Codex.

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.

2 participants