I use perpetual calendar code to calculate whether the current date is within the British Summer Time period (daylight savings) or not. The months April through September and November through February are easy: they’re BST or not. I use the code to find the last Sunday of March and of October, and compare that to the current day to see if the DST hour needs to be added or not. The code is not original; there are lots of perpetual calendar listing on the net.
PS. When looking at the code, remember Squirrel’s date() starts values at 0 not 1.
`// CALENDAR FUNCTIONS
function dayOfWeek(day, month, year)
{
local daysInMonth =
[
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
]
local absoluteDay = 0
local leap = 0
local a = 0
absoluteDay += ((year - 1) * 365)
absoluteDay += totalLeapDays(year)
leap = isLeapYear(year)
for (local i = 0; i < month; i++)
{
a = daysInMonth[leap]
absoluteDay += a[i]
}
absoluteDay += day
absoluteDay -= 11
return (6 + absoluteDay) % 7
}
function isLeapYear(year)
{
if ((year % 400) || ((year % 100) && !(year % 4))) return 1
return 0
}
function totalLeapDays(year)
{
local total = (year / 4)
if (isLeapYear(year)) total -= 1
total -= ((year / 100) - (1752 / 100))
total += ((year / 400) - (1752 / 400))
return total
}
function bstCheck()
{
// Should daylight savings be applied?
local bstFlag = false
local now = date()
// Months April through September use BST
if (now.month > 2 && now.month < 9) bstFlag = true
// In March, BST starts last Sunday
if (now.month == 2)
{
for (local i = 31 ; i > 23 ; i--)
{
// Iterate back through the days of March to find change
if (dayOfWeek(i, now.month, now.year) == 0)
{
if (now.day > i) bstFlag = true
break
}
}
}
// In October, BST ends last Sunday
if (now.month == 9)
{
for (local i = 31 ; i > 23 ; i--)
{
// Iterate forward through the days of October to find change
if (dayOfWeek(i, now.month, now.year) == 1)
{
if (now.day < i) bstFlag = true
break
}
}
}
return bstFlag
}`