473,387 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

"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,
-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
1 2759
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
Catalyst159
by: Catalyst159 | last post by:
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.