Daylight saving time question

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Wed Mar 21 08:06:34 CET 2007


On Mar 20, 12:53 pm, Mr Pekka Niiranen <pekka.niira... at 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




More information about the Python-list mailing list