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
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
6
Comments
—
Comments feed for this post
#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!!
#6 akaihola commented on 2012-10-26:
For Python timezone conversions, I use the handy table from the PyCon 2012 presentation by Taavi Burns.
Post a comment
About
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »
Search Blog
Tags
-
algorithms
(6)
-
android
(2)
-
aws
(10)
-
blogproject
(20)
-
c_cplusplus
(12)
-
cardstore
(8)
-
colinux
(2)
-
concurrency
(13)
-
conkeror
(2)
-
core
(2)
-
cygwin
(17)
-
datastructures
(15)
-
datetime
(4)
-
decorators
(4)
-
django
(41)
-
emacs
(22)
-
files_directories
(12)
-
git
(6)
-
hardware
(6)
-
install_setup
(8)
-
javascript
(3)
-
keyboard
(9)
-
matplotlib
(6)
-
mercurial
(4)
-
nginx
(2)
-
persistence
(6)
-
preferences
(7)
-
processes
(4)
-
pyqt
(18)
-
python
(157)
-
ratpoison
(3)
-
regexes
(6)
-
rsync
(3)
-
softwaretools
(17)
-
sql
(14)
-
ssh
(12)
-
subversion
(6)
-
twisted
(7)
-
ubuntu
(66)
-
urxvt
(5)
-
vxworks
(25)
-
webdev
(8)
-
wmii
(7)
Blogroll
- Adam Gomaa
- Alex Clemesha
- Amir Salihefendic
- Armin Ronacher
- David Beazley
- David Ziegler
- Duncan McGreggor
- Gareth Rushgrave
- Glyph Lefkowitz
- Guido van Rossum
- Ian Bicking
- Jacob Kaplan-Moss
- James Bennett
- James Tauber
- Jesper Noehr
- Marty Alchin
- Matt Harrison
- Nikolay Kolev
- Parand Darugar
- Peter Baumgartner
- Peter Bengtsson
- Rob Hudson
- Simon Willison
- Will McGugan
#1 GMTSlider.com commented on 2010-09-28:
Great! Thanks for this chunk of code! Exactly what I was looking for.