By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,378 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

"zoning" a naive datetime object / daylight savings

P: n/a
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 Monsen

Aug 31 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a
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.