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

Daylight saving time question

P: n/a
Hi,

is it possible to get the two annual daylight saving times
(day, month and time) from Python by giving location
in some country/location string ("Europe/Finland" for example).

I need to ask country in program and calculate daylight
saving times for the next few years onwards somehow like this:

for y in range(2007, 2017):
(m1,d1,t1,m2,d2,t2) = daylight_change_epochs("Finland")

-pekka-
Mar 20 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mar 20, 2:53 pm, Mr Pekka Niiranen <pekka.niira...@pp5.inet.fi>
wrote:
Hi,

is it possible to get the two annual daylight saving times
(day, month and time) from Python by giving location
in some country/location string ("Europe/Finland" for example).

I need to ask country in program and calculate daylight
saving times for the next few years onwards somehow like this:

for y in range(2007, 2017):
(m1,d1,t1,m2,d2,t2) = daylight_change_epochs("Finland")

-pekka-
I recommend you read this article about using the dateutil module:

http://labix.org/python-dateutil

I can't remember, but I think this one might have a slightly more
restrictive license than other modules. Be sure to check that as well.
The "tzoffset type" is probably what you need

Mike

Mar 20 '07 #2

P: n/a
On Mar 20, 12:53 pm, Mr Pekka Niiranen <pekka.niira...@pp5.inet.fi>
wrote:
Hi,

is it possible to get the two annual daylight saving times
(day, month and time) from Python by giving location
in some country/location string ("Europe/Finland" for example).

I need to ask country in program and calculate daylight
saving times for the next few years onwards somehow like this:

for y in range(2007, 2017):
(m1,d1,t1,m2,d2,t2) = daylight_change_epochs("Finland")

-pekka-


A generator defined via recursion:

import dateutil.rrule, dateutil.tz
import datetime

mytz = dateutil.tz.tzfile("/usr/share/zoneinfo/Europe/Helsinki")

start = datetime.datetime(2007,1,1,0,0,0,tzinfo=mytz)
end = datetime.datetime(2017,1,1,0,0,0,tzinfo=mytz)

successively_finer = {
dateutil.rrule.WEEKLY: dateutil.rrule.DAILY,
dateutil.rrule.DAILY: dateutil.rrule.HOURLY,
dateutil.rrule.HOURLY: dateutil.rrule.MINUTELY,
dateutil.rrule.MINUTELY: dateutil.rrule.SECONDLY
}

# find week, then day, then hour, etc. that spans a change in DST

def sieve (start, end, freq):
dstflag = start.timetuple()[-1]
iter = dateutil.rrule.rrule(freq,dtstart=start,until=end)
tprior = start
for t in iter:
if t.timetuple()[-1] != dstflag:
dstflag = t.timetuple()[-1]
if freq == dateutil.rrule.SECONDLY:
yield tprior, t
else:
yield sieve(tprior, t,
successively_finer[freq]).next()
tprior = t
raise StopIteration

for before, after in sieve(start, end, dateutil.rrule.WEEKLY):
print "%s =%s" % (
before.strftime("%Y-%m-%d %H:%M:%S (%a) %Z"),
after.strftime("%Y-%m-%d %H:%M:%S (%a) %Z"))
I get:

2007-03-25 02:59:59 (Sun) EET =2007-03-25 03:00:00 (Sun) EEST
2007-10-28 02:59:59 (Sun) EEST =2007-10-28 03:00:00 (Sun) EET
2008-03-30 02:59:59 (Sun) EET =2008-03-30 03:00:00 (Sun) EEST
2008-10-26 02:59:59 (Sun) EEST =2008-10-26 03:00:00 (Sun) EET
2009-03-29 02:59:59 (Sun) EET =2009-03-29 03:00:00 (Sun) EEST
2009-10-25 02:59:59 (Sun) EEST =2009-10-25 03:00:00 (Sun) EET
2010-03-28 02:59:59 (Sun) EET =2010-03-28 03:00:00 (Sun) EEST
2010-10-31 02:59:59 (Sun) EEST =2010-10-31 03:00:00 (Sun) EET
2011-03-27 02:59:59 (Sun) EET =2011-03-27 03:00:00 (Sun) EEST
2011-10-30 02:59:59 (Sun) EEST =2011-10-30 03:00:00 (Sun) EET
2012-03-25 02:59:59 (Sun) EET =2012-03-25 03:00:00 (Sun) EEST
2012-10-28 02:59:59 (Sun) EEST =2012-10-28 03:00:00 (Sun) EET
2013-03-31 02:59:59 (Sun) EET =2013-03-31 03:00:00 (Sun) EEST
2013-10-27 02:59:59 (Sun) EEST =2013-10-27 03:00:00 (Sun) EET
2014-03-30 02:59:59 (Sun) EET =2014-03-30 03:00:00 (Sun) EEST
2014-10-26 02:59:59 (Sun) EEST =2014-10-26 03:00:00 (Sun) EET
2015-03-29 02:59:59 (Sun) EET =2015-03-29 03:00:00 (Sun) EEST
2015-10-25 02:59:59 (Sun) EEST =2015-10-25 03:00:00 (Sun) EET
2016-03-27 02:59:59 (Sun) EET =2016-03-27 03:00:00 (Sun) EEST
2016-10-30 02:59:59 (Sun) EEST =2016-10-30 03:00:00 (Sun) EET

--
Hope this helps,
Steven

Mar 21 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.