Daylight saving time question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mr Pekka Niiranen

    #1

    Daylight saving time question

    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("Finlan d")

    -pekka-
  • kyosohma@gmail.com

    #2
    Re: Daylight saving time question

    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("Finlan d")
    >
    -pekka-
    I recommend you read this article about using the dateutil module:



    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

    Comment

    • attn.steven.kuo@gmail.com

      #3
      Re: Daylight saving time question

      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("Finlan d")
      >
      -pekka-


      A generator defined via recursion:

      import dateutil.rrule, dateutil.tz
      import datetime

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

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

      successively_fi ner = {
      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,dtst art=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_fi ner[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

      Comment

      Working...