(Corrected) IMP doc's error in sample code for date(integer)

Hi,

there is an error in the sample code provided for the date(integer) function located here… http://electricimp.com/docs/squirrel/system/date/

if (world_time_flag) { hours_count = hours_count + world_time_offset; if (hours_count > 24) hours_count = hours_count - 24; if (hours_count < 0) hours_count = 24 - hours_count; }
the last line of code needs to be changed to…

if (hours_count < 0) hours_count = 24 + hours_count

because if the hours count is less than zero then it is negative and 24 -(-x) = 24+x which results in a value >24 but the idea is to arrive at a value <24

While on the subject of Date and Time…
Is there a way for the IMP to determine if Daylight Saving Time (DST) is in effect??

That way the conversion from UTC to Local time could automatically “Spring Forward” or “Fall Back” as needed.

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();
`

I know this is a minor error in the example code for date(integer) but it should be corrected by the IMP developers…

Isn’t someone from IMP paying attention to this forum.
Hey IMP folks… any body home??

Or was I incorrect in thinking the sample code was incorrect??

dwight

Thanks for the catch @impulsepower (and for bugging/reminding us to change it). You are correct about how it works, and I’ve updated the GIST to reflect that.

There is not currently a way to detect if DST is in effect - although as @dheadrick mentioned, there are some date/time APIs that will return this information for you.

Thanks!
Issue corrected
And title of thread changed to reflect that.