473,386 Members | 1,835 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,386 software developers and data experts.

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("Finland")

-pekka-
Mar 20 '07 #1
2 1853
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Amitabh Deepak | last post by:
Is there any way to check whether daylight saving is enabled on a linux machine?
1
by: Drew | last post by:
Is there a way to check if it is daylight savings or not via c#? I have heard you can use System.Globalization? Thanks - Drew
7
by: Brett Edman | last post by:
I created a UTC clock using this: UTCTime = MyTime.ToUniversalTime() Now that we've turned the clocks ahead 1 hour for daylight savings time, the clock is reporting the wrong UTC time. It is...
1
by: maflatoun | last post by:
Hi everyone, I have 3 different time zones that I'm working with. Basically converting from UTC to those 3 time zones. Eastern Daylight Time (GMT -04:00, New York) Central Daylight Time (GMT...
27
by: RobG | last post by:
I was investigating a function to determine whether daylight saving was being observed on a particular date (given the platform's regional settings) and came across a suggestion at merlyn.com to...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.