2008-09-03

Using pytz to display a timestamp in a time zone

Download pytz from http://pytz.sourceforge.net/. You can try out the code snippet below without installing pytz. Just create your Python script in the directory where pytz's setup.py lives.

import time  
import pytz  
import datetime  
dt = datetime.datetime.fromtimestamp(time.time(), pytz.utc)  
#tz = pytz.timezone('EST')  
tz = pytz.timezone('CET')  
print tz.normalize(dt.astimezone(tz)).strftime('%Y-%m-%d %H:%M:%S %Z(%z)')

No comments: