SaltyCrane Blog —  Notes on Python and web development on Ubuntu Linux

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")


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")


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 ='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)


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



See also:

8 Comments — feed icon Comments feed for this post

#1 commented on 2010-09-28:

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

#2 Joe Kwiatkowski commented on 2011-01-27:

Smashing! Thank you.

#3 kamalakar commented on 2011-10-10:

It was very usefull thanks a lot :)

#4 RubyH20 commented on 2012-01-10:

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 2012-09-25:

You're Zen! Thank you

#6 akaihola commented on 2012-10-26:

For Python timezone conversions, I use the handy table from the PyCon 2012 presentation by Taavi Burns.

#7 Nathan Gould commented on 2013-08-19:

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

#8 Prateek Raj commented on 2015-01-13:

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

Post a comment

Required, but not displayed

Format using Markdown. (No HTML.)
  • Code blocks: prefix each line by at least 4 spaces or 1 tab (and a blank line before and after)
  • Code span: surround with backticks
  • Blockquotes: prefix lines to be quoted with >
  • Links: <URL>
  • Links w/ description: [description](URL)
Created with Django and Bootstrap | Hosted by Linode