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

naturaldelta with negative seconds loses precision and doesn't match naturaltime #18

Closed
Dapid opened this issue May 12, 2022 · 2 comments · Fixed by #173
Closed

naturaldelta with negative seconds loses precision and doesn't match naturaltime #18

Dapid opened this issue May 12, 2022 · 2 comments · Fixed by #173

Comments

@Dapid
Copy link

Dapid commented May 12, 2022

What did you do?

I passed a small negative integer value to naturaldelta, it reports "a day".

What did you expect to happen?

I expect naturaldelta to roughly match naturaltime. Else, if negative values are not supported, it should be documented.

What actually happened?

import humanize

print(humanize.naturaldelta(-1))
print(humanize.naturaltime(-1))

shows:

a day
a second ago

What versions are you using?

  • OS: Linux LCARS2 5.17.4-200.fc35.x86_64 #1 SMP PREEMPT Wed Apr 20 15:37:53 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
  • Python: 3.9.12
  • Humanize: 4.1.0
@carterbox
Copy link
Contributor

carterbox commented May 12, 2022

>>> dt.timedelta(seconds=-1)
datetime.timedelta(days=-1, seconds=86399)
>>> dt.timedelta(seconds=1)
datetime.timedelta(seconds=1)

I also didn't expect this behavior, but humanize.naturaldelta() matches the behavior of datetime.timedelta(). Or rather it's caused by the behavior of datetime.timedelta()? The question is whether it's correct to humanize "-1 days and 86k seconds" to "a day (ago)".

@carterbox
Copy link
Contributor

One solution could be to call abs() on the value parameter since a delta is neither negative or positive. abs() also works on dt.timedeltas.

@hugovk hugovk changed the title Naturaldelta with negative seconds looses precision and doesn't match naturaltime naturaldelta with negative seconds loses precision and doesn't match naturaltime May 19, 2022
@hugovk hugovk closed this as completed in 218a86e Feb 25, 2024
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 a pull request may close this issue.

2 participants