I used the API at timezonedb.com.
This is my agent code which checks the database. I had to add some logic so that I don’t just check the database every day all year because the site asks politely to not check too often. My code only checks the time every 2 hours and queries the actual database if the time is between 1am - 3am each day during a transition month. This equates to only checking daily for 2 months of the year.
`
// TimeZoneDB credentials for getting time offset
TIMEZONEDB_KEY <- “ENTERYOURKEYHERE”;
TIMEZONEDB_ZONE <- “America/Toronto”;
TIMEZONE_OFFSET <- 0; // Set initially to UTC so we’ll know if TZDB has been checked since restart
AVERAGE_OFFSET <- 4.5; // The average time zone offset in your area (EST+EDT)/2
//-------------------------------------------------------------------------
// Figure out the current timezone offset taking into account DST
// using the API at timezonedb (http://timezonedb.com/time-zones/America/Toronto)
//-------------------------------------------------------------------------
function timeZoneDB()
{
local url = “http://api.timezonedb.com/?zone=” + TIMEZONEDB_ZONE +
"&format=json&key=" + TIMEZONEDB_KEY;
http.get(url).sendasync(function(res)
{
if (res.statuscode == 200) {
local data = http.jsondecode(res.body);
if ("status" in data) {
if (data.status == "OK") {
TIMEZONE_OFFSET = data.gmtOffset.tointeger() / 3600;
server.log("Automatically set TZ (UTC" + TIMEZONE_OFFSET + ")");
} else {
server.log("TimeZoneDB returned: " + res.body);
}
}
}
});
}
function timeOffsetCheck()
{
local today = date( time() ); // Find todays UTC date/time in order to check if we need to adjust for DST
local month = today.month + 1;
local hour = today.hour - AVERAGE_OFFSET;
if (dailyTimer) {
imp.cancelwakeup(dailyTimer);
}
dailyTimer = imp.wakeup(7200, timeOffsetCheck ); // Check the time every 2 hours
if ( month < 3 || month > 11) {
if ( TIMEZONE_OFFSET == 0 ) {
TIMEZONE_OFFSET = -5; // Currently EST
server.log(“Manually set TZ to EST (UTC-5)”);
}
} else if ( month > 3 && month < 11) {
if ( TIMEZONE_OFFSET == 0 ) {
TIMEZONE_OFFSET = -4; // Currently EDT
server.log(“Manually set TZ to EDT (UTC-4)”);
}
} else {
if ( TIMEZONE_OFFSET == 0 ) {
timeZoneDB(); // A transition month (March/November)
} else if ( hour >= 1 && hour <= 3 ) {
timeZoneDB(); // A transition month (March/November)
}
}
}
timeOffsetCheck();
`