Daylight saving time question

M

Mr Pekka Niiranen

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-
 
K

kyosohma

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
 
A

attn.steven.kuo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top