Issues with odd months when restricting to "day" precision (precisedelta)
See original GitHub issueWhat did you do?
I’m trying to provide more precision than “months” when the timedelta
is more than 1 month, but I’m finding some issues using precisedelta
set to days
with a format of %d
as seen below. This all seems to stem from the use of 30.5 as a divisor for mod in precisedelta
and how it and ngettext
interact with floats vs. ints when it comes to pluralize and the display of the days itself. For what it’s worth, the same thing happens at second precision with microseconds present, etc. It seems to be at the minimum_unit
where this logic takes place.
What did you expect to happen?
>>> import humanize
>>> import datetime
>>> humanize.precisedelta(datetime.timedelta(days=31), minimum_unit="days", format="%d")
'1 month'
>>>
>>> import humanize
>>> import datetime
>>> humanize.precisedelta(datetime.timedelta(days=92), minimum_unit="days", format="%d")
'3 months'
>>>
>>> import humanize
>>> import datetime
>>> humanize.precisedelta(datetime.timedelta(days=32), minimum_unit="days", format="%d")
'1 month and 1 day'
>>>
What actually happened?
>>> import humanize
>>> import datetime
>>> humanize.precisedelta(datetime.timedelta(days=31), minimum_unit="days", format="%d")
'1 month and 0 days'
>>>
>>> import humanize
>>> import datetime
>>> humanize.precisedelta(datetime.timedelta(days=92), minimum_unit="days", format="%d")
'3 month and 0 days'
>>>
>>> import humanize
>>> import datetime
>>> humanize.precisedelta(datetime.timedelta(days=32), minimum_unit="days", format="%d")
'1 month and 1 days'
>>>
What versions are you using?
- OS: Windows, Ubuntu 20.04
- Python: 3.8.10
- Humanize: 4.1.0
Please include code that reproduces the issue (included in expected, actually happened above)
Issue Analytics
- State:
- Created a year ago
- Comments:10 (9 by maintainers)
Top Results From Across the Web
Issues · python-humanize/humanize - GitHub
naturaldelta with negative seconds loses precision and doesn't match ... Issues with odd months when restricting to "day" precision (precisedelta).
Read more >Python's humanize timedelta() tells me that minimum_unit is ...
Note: the months=True argument doesnt help, because it only forces the timedelta to be returned in months instead of days, when the difference ......
Read more >Time - humanize - Read the Docs
Return a precise representation of a timedelta. Instead, the minimum_unit can be changed to have a better resolution; the function will still readjust...
Read more >EFiled: Mar 5 2012 3:23PM EST Filing ID 42877110 Case ...
Plaintiffs waited a full nine months after filing the Complaint and six months after SPCC's shareholders approved the Merger to inquire about the...
Read more >Book Review: The Hungry Brain | Slate Star Codex
Likewise, when we eat too much food over the course of a few days, ... No, the body is not going to gain...
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
Actually
truncate=False
is what you want most of the time. Consider the following example:precisedelta
tries to not loose any precision so it returns0.50
(see docNow, if the user wants to use a interger-like formatting for the fractional part like
"%d"
, only then it makes sense to usetruncate=True
because otherwise you will get a0 units
:See doc and doc
Also, not very sure if
truncate
should be public… it feels a hack: the algorithm is okay but how to enable it is what it looks hacky. For example, assumingformat != '%d'
, is any useful case to settruncate=True
. I don’t think so.In an ideal world
truncate
should be automatically set fromformat
but not sure how to do it without doingif format == '%d' ...
(I’m not fully convinced to that, it is fragile). For example, forformat = '%i'
you have totruncate=True
also.@eldipa the
truncate
parameter seems to be removed from theprecisedelta
func. Any ideas how I can truncate the output without it?