Time and Date Not reporting correctly

Hi all,

    I have been using Electric Imp for a while now and I really like it. I am facing an issue with the Time and Date reporting. I am using date() function in my AGENT side of the device to get the UTC time but occasionally it reports erroneous time. I am not able to fix it as it is intermittent.

To extract the details out of the date() I am using:

var = date();
server.log(var.year);
server.log(var.month);
server.log(var.day);
server.log(var.hour);
server.log(var.min);
server.log(var.sec);

but the logs shows a widely different time than the current UTC time.

Kindly help.

Thanks,
Ravi

Can you give examples? Not sure how that could be wrong. Cut & paste from the log would be best as we can see the time on the log lines plus the logged numbers.

The current UTC time as I write this post is 17:12pm and the date is 08-26-2015.

I reformatted the entire string so that it appears on a single line as shown below.

local var, dateCreated;
var = date();

dateCreated = (var.year + “-” + var.month + “-” + var.day + " " + var.hour + “:” + var.min + “:” + var.sec);

and when I do a server.log(dateCreated); and run with couple of minutes delay it throws the logs like this.
2015-08-26 13:07:45 UTC-4 [Agent] 2015-7-26 7:7:41
2015-08-26 13:09:46 UTC-4 [Agent] 2015-8-26 9:9:43
2015-08-26 13:12:29 UTC-4 [Agent] 2015-8-25 0:12:28
2015-08-26 13:15:13 UTC-4 [Agent] 2015-7-26 17:12:28
2015-08-26 13:26:10 UTC-4 [Agent] 2015-8-26 8:26:8

Kindly help.

Thanks,
Ravi

Not sure why it’s not working for you. Here’s the code I just tried and it works fine:
`
function printTime(){
local d = date();
server.log(format("%d-%02d-%02d %d:%02d:%02d", d.year, (d.month+1), d.day, d.hour, d.min, d.sec));
imp.wakeup(5, printTime);
}

printTime();
`

Output:
2015-08-26 14:33:46 UTC-7 [Agent] 2015-08-26 21:33:46 2015-08-26 14:33:51 UTC-7 [Agent] 2015-08-26 21:33:51 2015-08-26 14:33:56 UTC-7 [Agent] 2015-08-26 21:33:56 2015-08-26 14:34:01 UTC-7 [Agent] 2015-08-26 21:34:01 2015-08-26 14:34:06 UTC-7 [Agent] 2015-08-26 21:34:06

Note that Month is zero indexed so if you want the human readable month you have to add 1.