Does anybody know of a good way to account for DST

I have a project where my garage door will send me a message if the door is open. In the message it informs me of the time the message was sent (in case the sending procedure takes a while to reach me… such as email). However, I would like this time to be correct for my local which experiences daylight savings time half the year.

http://www.timeanddate.com/time/change/canada/toronto

Is there an easy way to accomplish this as the start/stop time each year is on a different date?

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

}`

Would the Google geolocation API (at https://github.com/electricimp/reference/tree/master/webservices/google ) help? Look specifically at the function getTZdata.

Yes… that’s super useful. Thanks!

You can also use timezonedb, I wrote a bit code for working with that, and also included how to use it. :slight_smile:

What a coincidence. I was just heading back here to comment that I found that just now. I actually grabbed your Mailgun idea for my garage door app as well. Very useful stuff.

I read about your pushover implementation at the same time. My garage door code actually has functions within it for contacting me using Twilio/Mailgun/Pushover/Sendhub, but all but Mailgun are commented out at the moment. Just wanted to futureproof it as much as possible.

Thanks!