SaltyCrane Blog — Notes on JavaScript and web development

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:

Comments


#1 GMTSlider.com commented on :

Great! Thanks for this chunk of code! Exactly what I was looking for.


#3 kamalakar commented on :

It was very usefull thanks a lot :)


#4 RubyH20 commented on :

Wow, I have looked at a ton of TZ conversions for Python; this is by far the best example. Thank you!!


#5 bala commented on :

You're Zen! Thank you


#7 Nathan Gould commented on :

Very helpful. This info is surprisingly hard to find elsewhere online, so thanks!


#8 Prateek Raj commented on :

Thanks! Exactly what I needed. Was having a lot of trouble finding this anywhere else!


#9 Dharmit Shah commented on :

Almost 6 years since you first wrote the post. Still a lot helpful! Thanks for this article Eliot.

disqus:1915537686


#10 swathi commented on :

convert local time to colambia time in us

disqus:2059625780


#11 swathi commented on :

hi nick, how to convert our local time to colambia time in u.s

disqus:2059639667


#12 shayneo commented on :

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