$ python --version
Python 2.7.12
When I tried to compare the time of the card obtained by Trello's API with the current time, I got an error when I wrote the following code and was addicted to it.
def show_cards_(cards):
for c in cards:
print("---------------")
print("card id : {id}".format(id=c.id))
print("card name : {name}".format(name=c.name))
if c.due is not None:
due = c.due_date
now = dt.now()
print("It's passed" if now > due else "Still okay")
When I ran this code, I got the following error:
TypeError: can't compare offset-naive and offset-aware datetimes
It seems that due and now cannot be compared because they are different for offset-native and offset-aware, respectively.
Looking closely at the contents, it looked like this.
2017-01-03 10:00:00+00:00 #due: offset-aware
2017-01-03 22:40:14.709333 #now: offset-native
It was offset-aware utc time that trello returned.
In python, it seems that it will be compared according to either one.
Do not mix ʻoffset-native`` and
ʻoffset-aware``.
I didn't care at all until now, but it seems that pytz
and python-dateutil
will take care of me at such times.
>>> from datetime import datetime as dt
>>> print(dt.now())
2017-01-03 22:45:07.334307+09:00
In most cases this is ok
>>> from datetime import datetime as dt
>>> import dateutil.tz
>>> print(dt.now(dateutil.tz.tzlocal()))
2017-01-03 22:47:27.712684+09:00
You can fix the time zone to ```Asia / Tokyo``, but The system local timezone looked cool, so I tried it.
>>> from datetime import datetime as dt
>>> import pytz
>>> now_utc = dt.now(pytz.utc)
>>> print(now_utc)
2017-01-03 14:07:54.861061+00:00
It seems ok if you pass the timezone as a variable to the usual dt.now ()
.
↓ And
UTC → JST
>>> print(dt.now(pytz.utc).astimezone(pytz.timezone("Asia/Tokyo")))
2017-01-03 23:05:53.765549+09:00
Home! Home! Polkadot Stingray and DAOKO are the best!
Recommended Posts