Converting time zones for datetime objects in Python
Install pytz¶
I am using pytz, which is a time zone definitions package. You can install it using Easy Install. On Ubuntu, do this:
sudo easy_install --upgrade pytz
Add time zone information to a naive datetime object¶
from datetime import datetime
from pytz import timezone
date_str = "2009-05-05 22:28:15"
datetime_obj = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
datetime_obj_utc = datetime_obj.replace(tzinfo=timezone('UTC'))
print datetime_obj_utc.strftime("%Y-%m-%d %H:%M:%S %Z%z")
Results:
2009-05-05 22:28:15 UTC+0000
Add non-UTC time zone information to a naive datetime object¶
(Added 2014-05-28)NOTE: datetime.replace()
does not handle daylight savings time correctly. The correct way is to use timezone.localize()
instead. Using datetime.replace()
is OK when working with UTC as shown above because it does not have daylight savings time transitions to deal with. See the pytz documentation.
from datetime import datetime
from pytz import timezone
date_str = "2014-05-28 22:28:15"
datetime_obj_naive = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
# Wrong way!
datetime_obj_pacific = datetime_obj_naive.replace(tzinfo=timezone('US/Pacific'))
print datetime_obj_pacific.strftime("%Y-%m-%d %H:%M:%S %Z%z")
# Right way!
datetime_obj_pacific = timezone('US/Pacific').localize(datetime_obj_naive)
print datetime_obj_pacific.strftime("%Y-%m-%d %H:%M:%S %Z%z")
Results:
2014-05-28 22:28:15 PST-0800 2014-05-28 22:28:15 PDT-0700
Convert time zones¶
from datetime import datetime
from pytz import timezone
fmt = "%Y-%m-%d %H:%M:%S %Z%z"
# Current time in UTC
now_utc = datetime.now(timezone('UTC'))
print now_utc.strftime(fmt)
# Convert to US/Pacific time zone
now_pacific = now_utc.astimezone(timezone('US/Pacific'))
print now_pacific.strftime(fmt)
# Convert to Europe/Berlin time zone
now_berlin = now_pacific.astimezone(timezone('Europe/Berlin'))
print now_berlin.strftime(fmt)
Results:
2009-05-06 03:09:49 UTC+0000 2009-05-05 20:09:49 PDT-0700 2009-05-06 05:09:49 CEST+0200
List time zones¶
There are 559 time zones included in pytz. Here's how to print the US time zones:
from pytz import all_timezones
print len(all_timezones)
for zone in all_timezones:
if 'US' in zone:
print zone
Results:
US/Alaska US/Aleutian US/Arizona US/Central US/East-Indiana US/Eastern US/Hawaii US/Indiana-Starke US/Michigan US/Mountain US/Pacific US/Pacific-New US/Samoa
See also:¶
- http://docs.python.org/library/datetime.html - Python datetime module documentation
- http://uswaretech.com/blog/2009/02/understanding-datetime-tzinfo-timedelta-timezone-conversions-python/ - More info on tzinfo and datetime objects.
Related posts
- How to get the date N days ago in Python — posted 2010-10-16
- Python datetime / time conversions — posted 2008-11-12
- How to get the current date and time in Python — posted 2008-06-26
Comments
Great! Thanks for this chunk of code! Exactly what I was looking for.
Smashing! Thank you.
It was very usefull thanks a lot :)
Wow, I have looked at a ton of TZ conversions for Python; this is by far the best example. Thank you!!
You're Zen! Thank you
For Python timezone conversions, I use the handy table from the PyCon 2012 presentation by Taavi Burns.
Very helpful. This info is surprisingly hard to find elsewhere online, so thanks!
Thanks! Exactly what I needed. Was having a lot of trouble finding this anywhere else!
Almost 6 years since you first wrote the post. Still a lot helpful! Thanks for this article Eliot.
disqus:1915537686
convert local time to colambia time in us
disqus:2059625780
hi nick, how to convert our local time to colambia time in u.s
disqus:2059639667
I've taken to using the "arrow" library and its immensely easier than the mess that is pythons time handling. Theres still a few flaws in it around handling some datestrings, but combined with datetime its a godsend
disqus:2488386621