467,887 Members | 1,582 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,887 developers. It's quick & easy.

"zoning" a naive datetime object / daylight savings

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,

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

Aug 31 '05 #1
  • viewed: 2143
1 Reply
Ok, I think I figured this out. Comments/criticisms welcome.

import datetime, os, time
from pytz import UTC
old_tz = os.environ.get('TZ')
os.environ['TZ'] = 'Europe/Amsterdam'
dutchDateParts = (2005, 8, 10, 17, 26, 0, 2, 222, -1)
timestamp = time.mktime(dutchDateParts)
if old_tz: os.environ['TZ'] = old_tz
else: del os.environ['TZ']
print datetime.datetime.fromtimestamp(timestamp, UTC)
The 9-integer time tuple could be fetched using the code posted here:
same URL, not snipped:

Adam Monsen

Sep 1 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.