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

The package (wrongly) assumes all times are in the local timezone #55

Closed
JanLikar opened this issue May 1, 2017 · 3 comments · Fixed by #144
Closed

The package (wrongly) assumes all times are in the local timezone #55

JanLikar opened this issue May 1, 2017 · 3 comments · Fixed by #144

Comments

@JanLikar
Copy link

JanLikar commented May 1, 2017

No description provided.

@JanLikar JanLikar changed the title The package (wrongly) assumes all times are in the local timezonen The package (wrongly) assumes all times are in the local timezone May 1, 2017
@shaleh
Copy link

shaleh commented Aug 17, 2017

Can you provide example failures and expected results?

@kippandrew
Copy link

kippandrew commented Apr 4, 2018

The issue seems to be the naturaltime function (and possibly others) assumes that a datetime object that is passed is timezone naive (meaning it has no tzinfo). If you do pass a timezone aware datetime, an error is raised because you can't compare a timezone naive and timezone aware datetime object.

A solution might be to allow the caller to pass in the 'now' time.

On Python 3.5:

>>> import datetime
>>> import humanize

# parse a timestamp with utc offset
>>> ts = datetime.datetime.strptime("2018-02-15T21:31:16.048820-0800", "%Y-%m-%dT%H:%M:%S.%f%z")

>>> ts
datetime.datetime(2018, 2, 15, 21, 31, 16, 48820, tzinfo=datetime.timezone(datetime.timedelta(-1, 57600)))

>>> humanize.naturaltime(ts)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/akipp/.virtualenvs/lucid-kube/lib/python3.5/site-packages/humanize/time.py", line 116, in naturaltime
    date, delta = date_and_delta(value)
  File "/Users/akipp/.virtualenvs/lucid-kube/lib/python3.5/site-packages/humanize/time.py", line 30, in date_and_delta
    delta = now - value
TypeError: can't subtract offset-naive and offset-aware datetimes```

@kippandrew
Copy link

Another solution would be to allow the caller to pass in tzinfo struct and localize the "now" time to that tz.

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.

3 participants