, ,

As a bit of background, I have a bunch of UTC datetimes in a database, but for reporting purposes, I would like to see those times in a local time. I was curious as to whether pytz would know to make adjustments for daylight savings time automatically, so I wrote this little test program.


import datetime
import pytz

def report(time):
    detroittz = pytz.timezone('America/Detroit')
    easterntz = pytz.timezone('US/Eastern')

    dtformat = "%Y-%m-%d %H:%M"
    t = datetime.datetime.strptime(time, dtformat).replace(tzinfo=pytz.utc)
    print "*****"
    print t
    print detroittz.normalize(t)
    print easterntz.normalize(t)

report("2011-12-21 00:00")
report("2011-06-21 00:00")

It produces the output:

2011-12-21 00:00:00+00:00
2011-12-20 19:00:00-05:00
2011-12-20 19:00:00-05:00
2011-06-21 00:00:00+00:00
2011-06-20 20:00:00-04:00
2011-06-20 20:00:00-04:00

And so the answer is, yes. The date which is outside of the DST period is reported as UTC-5, and the date inside the DST period is reported as UTC-4. Very nice.