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

TypeError: can't subtract offset-naive and offset-aware datetimes #122

Closed
ErikKalkoken opened this issue Jun 30, 2023 · 3 comments · Fixed by #123
Closed

TypeError: can't subtract offset-naive and offset-aware datetimes #122

ErikKalkoken opened this issue Jun 30, 2023 · 3 comments · Fixed by #123

Comments

@ErikKalkoken
Copy link
Contributor

ErikKalkoken commented Jun 30, 2023

What did you do?

I wanted to calculate naturaltime between two timezone aware datatimes.

What did you expect to happen?

Get a string the with naturaltime between those datetimes.

What actually happened?

A TypeError exception was raised:

Traceback (most recent call last):
  File "/home/erik/python/projects/aa-dev/aa-memberaudit/temp/humanize_bug.py", line 8, in <module>
    result = humanize.naturaltime(same_dt, when=my_now)
  File "/home/erik/python/projects/aa-dev/venv/lib/python3.10/site-packages/humanize/time.py", line 253, in naturaltime
    date, delta = _date_and_delta(value, now=now)
  File "/home/erik/python/projects/aa-dev/venv/lib/python3.10/site-packages/humanize/time.py", line 76, in _date_and_delta
    delta = now - value
TypeError: can't subtract offset-naive and offset-aware datetimes

What versions are you using?

  • OS: Ubuntu 22.04.2
  • Python: 3.10
  • Humanize: 4.7.0

Please include code that reproduces the issue.

The best reproductions are self-contained scripts with minimal dependencies.

import datetime as dt

import humanize

my_now = dt.datetime.now(tz=dt.timezone.utc)
same_dt = my_now - dt.timedelta(days=3, hours=1)

result = humanize.naturaltime(same_dt, when=my_now)

Additional information

This bug was introduced with 4.7, the code works fine with 4.6.

As a workaround one could remove the when=my_now part (a workaround to get it the function working with aware datetimes), but then it no longer works with humanize < 4.7, which would make 4.7 a breaking change. And it would also not work for when when is used in any other way.

ErikKalkoken added a commit to ErikKalkoken/humanize that referenced this issue Jun 30, 2023
ErikKalkoken added a commit to ErikKalkoken/humanize that referenced this issue Jun 30, 2023
lemon24 added a commit to lemon24/reader that referenced this issue Jul 15, 2023
jan-di added a commit to jan-di/toggl-track-companion that referenced this issue Jul 17, 2023
@sshaikh
Copy link

sshaikh commented Aug 7, 2023

Do we know what the expected release/date is for this fix? I'm trying to decide if I should release pinning to 4.6 or wait.

@hugovk
Copy link
Member

hugovk commented Aug 7, 2023

I've not had chance to check the PR yet, and no-one else has reviewed either. Would you be able to check it? I could probably release next week if all is well. Thanks!

@shadchin
Copy link

shadchin commented Aug 8, 2023

I use patch from #123 , it works fine

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.

4 participants