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

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/

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.

----------------------------------8<----------------------------------
import datetime, os, time
from pytz import UTC
old_tz = os.environ.get('TZ')
os.environ['TZ'] = 'Europe/Amsterdam'
time.tzset()
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']
time.tzset()
print datetime.datetime.fromtimestamp(timestamp, UTC)
---------------------------------->8----------------------------------
The 9-integer time tuple could be fetched using the code posted here:
http://snipurl.com/hcvs
same URL, not snipped:
http://groups.google.com/group/comp....d8f116dfd59dd1

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

Sep 1 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.