Skip to content

naturaldelta() renders negative timedeltas as positive durations #535

Not planned

Description

@SonicHedghog

Summary

naturaldelta() silently erases the sign of negative timedeltas, rendering them as positive durations.

Reproduction (humanize 4.16.0)

import datetime, humanize

humanize.naturaldelta(datetime.timedelta(days=-400))
# '1 year, 1 month'   <-- no negative indication; 400 days in the PAST
#                       renders identically to +400 days

humanize.naturaldelta(datetime.timedelta(seconds=-1))
# 'a second'   <-- again positive rendering

Why this seems wrong

-400 days and +400 days are opposite-signed durations but render identically. As with precisedelta() (filed separately), this arises naturally from swapped-operand arithmetic (end - start) and the sign erasure hides the error.

Possible resolutions

  • Render negative durations with a sign or negative phrasing; or
  • Document negatives as out of domain and raise rather than emit a wrong positive duration.

Activity

  1. SonicHedghog commented on Dec 2, 2026

    @SonicHedghog
    Author

    If a behavior change is welcome, I’d be happy to implement it with tests. If negative-timedelta normalization is intentional, I can instead prepare a narrow documentation clarification. I’ll wait for maintainer direction before opening a PR.

  2. SonicHedghog commented on Dec 2, 2026

    @SonicHedghog
    Author

    I re-checked both issues and PR history. For naturaldelta, #18 and #171 were earlier negative-timedelta reports, and merged #173 deliberately normalized the whole timedelta with abs(). I missed that context. Sorry for the noise. I’m closing this and won’t pursue a behavior-change or further PR without maintainer invitation.

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