Is it Daylight Saving TIme yet?

I needed a script that determined if Daylight Saving Time (US rules) was in effect. DST starts the 2nd Sunday in March, stops the 1st Sunday in November. I didn’t need it to the hour and minute. This is what I came up with:


on DST()
	set |year| to year of (current date)
	set |month| to (month of (current date)) * 1
	set |day| to day of (current date)
	set SundayCount to 0
	set IsDST to false -- months 1, 2, 12
	if |month| = 3 then
		repeat with i from 1 to |day|
			set TestDay to date (|month| & "/" & i & "/" & |year| as text)
			if weekday of TestDay = Sunday then set SundayCount to SundayCount + 1
			if SundayCount = 2 then
				set IsDST to true
				exit repeat
			end if
		end repeat
	end if
	if (|month| > 3 and |month| < 11) then set IsDST to true
	if |month| = 11 then
		repeat with i from 1 to |day|
			set TestDay to date (|month| & "/" & i & "/" & |year| as text)
			if weekday of TestDay = Sunday then set SundayCount to SundayCount + 1
			if SundayCount = 1 then exit repeat
		end repeat
		if SundayCount = 0 then set IsDST to true
	end if
	return IsDST
end DST

DST() returns a boolean (IsDST) true if DST is in effect, false if not. Wishing Applescript had a CASE structure, but anyway…

…Mick

Hi,

in Mavericks the most comfortable solution is an AppleScriptObjC handler which can be used in a Script Library


on isDaylightSavingTime()
	return current application's NSTimeZone's systemTimeZone()'s isDaylightSavingTime() as boolean
end isDaylightSavingTime

For Europe (CEST) it’s just

-- when time to GMT is 3600 it means it's normal time, when it's 7200 it's summertime
set isDST to time to GMT is not 3600

Stefan, DJ -

Thank you. Elegant both ways. For U.S. readers and others, remember that our time to GMT is a negative number, and adjust. Using the east coast as an example, time to GMT goes from -18000 to - 14400 the second Sunday in March.

…Mick

You have a special definition of Europe !

Great Britain, Ireland and Iceland are in Europe but they are GMT 0

Finland, Lithuania, Latvia, Estonia, Belarus, Ukraine, Moldova, Romania, Bulgaria, Greece, Cyprus are also in Europe but are GMT +2

Yvan KOENIG (VALLAURIS, France) vendredi 14 mars 2014 18:56:46

Another vanilla offering:

on isDSTinUS()
	set now to (current date)
	
	-- Calculate when this year's DST starts. The second Sunday of any month occurs between the 8th and the 14th, so get the 14th March and subtract the number of days since Sunday.
	copy now to dstStart
	tell dstStart to set {its day, its month, its time} to {14, March, 0} -- If you know the correct time for the start of DST, use that instead.
	set dstStart to dstStart - ((dstStart's weekday) - 1) * days
	
	-- Similarly, calculate the end of this year's DST from 7th November
	copy now to dstEnd
	tell dstEnd to set {its day, its month, its time} to {7, November, 0} -- Ditto with the time.
	set dstEnd to dstEnd - ((dstEnd's weekday) - 1) * days
	
	return ((now ≥ dstStart) and (now ≤ dstEnd))
end isDSTinUS

isDSTinUS()

That’s why I put the time zone between parentheses to let everyone know I meant central Europe time zone. It seems that you didn’t understand that :wink:

The point is that the time difference changes between GMT and Local time when you enter DST. So when you live in London and the time difference is 3600 you know it’s summer time, when it’s 0 it’s normal time. In Europe, when you enter or leave summer time, you change time zone, the timezone itself never changes its time. Determine the summer time by time difference between GMT is the easiest way to check locally if the current time is DST or not.