How to make a timezone aware datetime object
What I need to do
What I need to do
I would like to know what are all the possible values for the timezone argument in the Python library pytz. How to do it?
>>> import pytz >>> pytz.timezone('Asia/Hong_Kong') <DstTzInfo 'Asia/Hong_Kong' LMT+7:37:00 STD> A seven hour and 37 minute offset? This is a little strange, does anyone experience the same issue? In fact I’m getting different behavior between import pytz from datetime import datetime hk = pytz.timezone('Asia/Hong_Kong') dt1 = datetime(2012,1,1,tzinfo=hk) dt2 = hk.localize(datetime(2012,1,1)) if dt1 > dt2: print "Why?" … Read more
The best I can come up with for now is this monstrosity:
I’m having some weird issues with pytz’s .localize() function. Sometimes it wouldn’t make adjustments to the localized datetime:
We can use time.tzname get a local timezone name, but that name is not compatible with pytz.timezone.
I am try creating a datetime object in python using datetime and pytz, the offset shown is wrong.
This is just another post on pytz.
The first is to use the localize() method provided by the pytz library. This is used to localize a naive datetime (datetime with no timezone information):
I’m trying to understand why I’m getting these results when converting time zones to UTC: