question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issues with odd months when restricting to "day" precision (precisedelta)

See original GitHub issue

What 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:open
  • Created a year ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
eldipacommented, May 19, 2022

Actually truncate=False is what you want most of the time. Consider the following example:

>>> humanize.precisedelta(datetime.timedelta(days=31), minimum_unit='days')
'1 month and 0.50 days'

precisedelta tries to not loose any precision so it returns 0.50 (see doc

Now, if the user wants to use a interger-like formatting for the fractional part like "%d", only then it makes sense to use truncate=True because otherwise you will get a 0 units:

>>> humanize.precisedelta(datetime.timedelta(days=31), minimum_unit='days', format='%d')
'1 month and 0 days'

>>> humanize.precisedelta(datetime.timedelta(days=31), minimum_unit='days', format='%d', truncate=True)
'1 month'

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, assuming format != '%d', is any useful case to set truncate=True. I don’t think so.

In an ideal world truncate should be automatically set from format but not sure how to do it without doing if format == '%d' ... (I’m not fully convinced to that, it is fragile). For example, for format = '%i' you have to truncate=True also.

0reactions
shazib-summarcommented, Nov 13, 2022

@eldipa the truncate parameter seems to be removed from the precisedelta func. Any ideas how I can truncate the output without it?

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found