镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

@Dapid

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

Activity

  1. carterbox commented on May 12, 2022

    @carterbox
    Contributor
    >>> 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)".

  2. carterbox commented on May 12, 2022

    @carterbox
    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.

  3. 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[/+] on May 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions