Skip to content

naturaltime should support timezone-aware datetimes #17

@snarfed

Description

@snarfed

Hi all! Reopening this one here: naturaltime currently breaks on timezone-aware datetime input values, eg with tzinfo set. Lots of background and details in jmoiron/humanize#9, jmoiron/humanize#190, and jmoiron/humanize#22, among others.

Example stacktrace below. Reproduces on the latest released version, 4.1.0. Thank you for maintaining humanize, it's great!

>>> from datetime import datetime, timezone
>>> from humanize import naturaltime
>>> naturaltime(datetime.now(timezone.utc))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ryan/src/bridgy/local/lib/python3.9/site-packages/humanize/time.py", line 231, in naturaltime
    date, delta = _date_and_delta(value, now=now)
  File "/Users/ryan/src/bridgy/local/lib/python3.9/site-packages/humanize/time.py", line 71, in _date_and_delta
    delta = now - value
TypeError: can't subtract offset-naive and offset-aware datetimes

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