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…
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.
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
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.