When Amy makes a comment in Taiwan at 20:00(GMT+8), his friend Tom in Japan should sees the comment is at at 21:00(GMT+9).
Best Practice:
- Please remember using
datetime.datetime.utcnow()to express object’s created time - Recommend that
pytzis awesome libary to handle timezone
In general, timezone is made from system time according to region of the globe.
dateime object difference between built-in library and Django
Note that time.time() and datetime.datetime.now() these functions are had adding time offset of timezone but without timezone information.
1 | import time |
If you are developing in Django, the timezone can definitely show tzinfo. That can be convenience to get datetime object with tzinfo and compare the difference.
1 | from django.utils import timezone |
In the above examples, I am located in GMT+8 of timezone, the UTC dateimte object adding timezone offset equals to the datetime generated by Python built-in library without timezone.
pytz is your friend
Expressing correct datetime object with tzinfo
In order to properly expressing tzinfo of datetime generated by Python built-in library, you can mark the datetime object using pytz.
1 | import datetime |
Using astimezone to adjust datetime
There is a function that is astimezone in datetime object, it can adjust datetime according to the new tzinfo.
1 | dt.astimezone(pytz.utc) |
Storing in Database
If you are developing an web application with database, please keep to the rule: storing datetime must be from UTC timezone. When you have many web application server and deploy in different region, it should consider that creating a object with a timestamp must avoid to affecting by timezone, otherwise the object shows in other region and adjusts by timezone of the region, the datetime expression will absolutely make a mistake.