Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

precisedelta with minimum unit seconds fails when seconds is a very small value #159

Closed
riisi opened this issue Sep 23, 2020 · 2 comments · Fixed by #170
Closed

precisedelta with minimum unit seconds fails when seconds is a very small value #159

riisi opened this issue Sep 23, 2020 · 2 comments · Fixed by #170
Labels
bug Something isn't working

Comments

@riisi
Copy link

riisi commented Sep 23, 2020

What did you do?

Calculated a small delta.

import humanize
from datetime import timedelta
duration_seconds = 1e-06       # A small value, or 0
delta = timedelta(seconds=duration_seconds)
output = humanize.precisedelta(delta, minimum_unit="minutes")
print(output)

What did you expect to happen?

Output would be 0 seconds.

Alternatively, a better error message or a warning in the documentation would be helpful.

What actually happened?

>       tail = texts[-1]
E       IndexError: list index out of range

What versions are you using?

  • OS: ubuntu
  • Python: 3.6.12
  • Humanize: 2.6.0
@hugovk hugovk added the bug Something isn't working label Sep 23, 2020
@hugovk
Copy link
Collaborator

hugovk commented Sep 23, 2020

precisedelta was added in #137.

@eldipa Please could you have a look?

@eldipa
Copy link
Contributor

eldipa commented Oct 19, 2020

What it is happening is that if the delta is in microseconds (lower than 1 second to be precise) and the minimum unit is greater than seconds (like minutes), the microseconds are not propagated to the upper units (like minutes) so it computes "0".

Because the function then ignores all the zeros (so we avoid "0 years, 0 hours, 3 secs"), the function crashes later because it deleted all the values (like "0 years, 0 hours").

The most sane fix is to catch this corner case and say "0 minutes" for example.

I left a PR with the proposed fix (sorry for the delay).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants