"zoning" a naive datetime object / daylight savings

Adam Monsen haircut at gmail.com
Wed Aug 31 17:32:23 EDT 2005


Say I have the following datetime[1] object:

>>> from datetime import datetime
>>> d = datetime(2005, 8, 10, 15, 43)

I happen to know this is a local time from the Netherlands[2], so I
assume the tzinfo (if it were present) should indicate Central European
Summer Time[3] ("Summer" indicates daylight savings).

How do I convert this date/time information to UTC?

Using pytz[4], it appears that I should be able to do the following:

>>> from datetime import datetime
>>> from pytz import timezone
>>> Netherlands = timezone('Europe/Paris')
>>> d = datetime(2005, 8, 10, 15, 43).replace(tzinfo=Netherlands)

But this object has unexpected timezone information, and converts to
UTC by losing 9 minutes instead of 2 hours!

>>> d.tzinfo
<DstTzInfo 'Europe/Paris' PMT+0:09:00 STD>
>>> d.astimezone(timezone('UTC'))
datetime.datetime(2005, 8, 10, 15, 34, tzinfo=<UTC>)

What I would *expect* to see is this:

>>> d.tzinfo
<DstTzInfo 'Europe/Paris' CEST+2:00:00 DST>
>>> d.astimezone(timezone('UTC'))
datetime.datetime(2005, 8, 10, 13, 43, tzinfo=<UTC>)

Any pointers?

I feel like pytz/python should be smart enough to know that since the
date/time is 15:43 on August 10th, 2005 in the Europe/Paris timezone
that daylight savings is in effect, and the equivalent UTC datetime
object should contain a time two hours prior. At least, that's what I
like to happen.

Also, anyone know if there is a more appropriate choice for timezone
than "Europe/Paris" for times in the Netherlands?

Thank you,
-Adam

References:
1. http://docs.python.org/lib/datetime-datetime.html
2. http://en.wikipedia.org/wiki/Netherlands
3. http://en.wikipedia.org/wiki/Central_European_Time
4. http://pytz.sf.net

-- 
Adam Monsen
http://adammonsen.com/




More information about the Python-list mailing list