[BangPypers] Timezone issues with datetime module.

kushal gupta kushalnitt at gmail.com
Wed Mar 10 15:52:50 CET 2010


Hi,
  I am facing an issue with datetime module with some timezones (especially
day light savings).
Given the date and the timezone, I want to convert it to the UTC date and
time.

Here is the snippet:

import datetime
import mx.DateTime
from dateutil import zoneinfo
def parse_date(date, tzname):
    feed_time =  mx.DateTime.strptime(date, '%d-%m-%Y')
    feed_datetime = datetime.datetime(*feed_time.timetuple()[:6],
tzinfo=zoneinfo.gettz(tzname))
    feed_time = mx.DateTime.DateTime(*feed_datetime.utctimetuple()[:6])

    return feed_time

>>>parse_date('28-03-2010', 'Australia/Melbourne')
<mx.DateTime.DateTime object for '2010-03-27 13:00:00.00' at 7f965cf55d98>
>>> parse_date('29-03-2010', 'Australia/Melbourne')
<mx.DateTime.DateTime object for '2010-03-28 14:00:00.00' at 7f965cf673f8>

which says day light savings ended on 28th March (seeing at the outputs,
there is a difference of 1 hr in time) but it actually ended on April 4th
http://www.timeanddate.com/worldclock/city.html?n=152


for  'America/Anchorage' the same code works fine (DST starts on 14th March
02:00 hours)
http://www.timeanddate.com/worldclock/city.html?n=18

>>> parse_date('14-03-2010', 'America/Anchorage')
<mx.DateTime.DateTime object for '2010-03-14 09:00:00.00' at 87fef8>
>>> parse_date('15-03-2010', 'America/Anchorage')
<mx.DateTime.DateTime object for '2010-03-15 08:00:00.00' at 87fef8>

What am i missing here?

Please help.
Thanks Kushal.


More information about the BangPypers mailing list