humanize overflows before datetime does
See original GitHub issueWhat did you do?
I tried to humanize a very long time.
What did you expect to happen?
I expect the time to be converted into years/millenia/eon if it is within the bounds of datetime i.e. <= 999 999 999 days, and if the number is higher than that or uncountable with the compute platform (64/32 bits) then something like “longer than countable”.
At the very least, this type of error should be handled like others in _date_and_delta
: the timedelta is returned unaltered.
What actually happened?
I received an overflow error from an operation within humanize.
What versions are you using?
- OS: Ubuntu x64
- Python: 3.9.6
- Humanize: 3.13.1
Please include code that reproduces the issue.
The best reproductions are self-contained scripts with minimal dependencies.
import datetime as dt
import humanize
d = dt.timedelta(days=999999999)
print(d)
h = humanize.naturaldelta(d)
print(h)
# ... e/time.py", line 131, in _date_and_delta
# date = now - value
# OverflowError: date value out of range
Once we discuss how to best handle this error, I’d be happy to open a PR.
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Python's humanize timedelta() tells me that minimum_unit ...
After running pip install humanize --upgrade I was able to use the precisedelta function suggested in the accepted answer. python · datetime ......
Read more >Moment.js | Docs
Before parsing a RFC 2822 date time the string is cleansed to remove any comments and/or newline characters. The additional characters are legal...
Read more >humanize 4.4.0
Python humanize utilities. ... /pkg/h/humanize/humanize-banner.webp ... are wrong/incomplete; humanize overflows before datetime does ...
Read more >Date and time of an article in a humanized and localized way
Put a space after now and before = info = None. Info will always be assigned something later, there is no reason to...
Read more >Carbon - A simple PHP API extension for DateTime.
$unixTimestamp. returns Carbon. Set the instance's timestamp. Timestamp input can be given as int, float or a string containing one or more numbers....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
To summarize: the original issue is that an OverflowError is raised by
naturaldelta()
at deltas much smaller than what datetime can handle. Other errors innaturaldelta()
cause a timedelta to be returned instead of a string, but this is undocumented. We want to retain the current behavior (to raise errors sometimes, return string sometimes, and return timedelta sometimes), but we want to document when each of these outcomes occurs.Can I still refactor
naturaldelta()
so that the OverflowError is raised by datetime instead of by humanize? I believe this is compatible with the approach above given the unrelated deprecation ofnaturaldelta(..., when)
.✅