Measure Real Time with Imp using Server/Agent time

Month returning an integer between 0 and 11 is actually not an uncommon programming construct…

The reason why is so that you can do this:

`Months <- [“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sept”, “Oct”, “Nov”, “Dec”];

server.log(Months[date.month]);`

Sorry all, but is the date <- date(); Converting the Unix timestamp to Readable Date/time?

if I use http://www.onlineconversion.com/unix_time.htm and enter the time 1387325188 I get back month 12,
if I was to enter the month 11 I get back a Unix timestamp of 1384733188.
I was going to use the date <- date(); to help log the date and time when the device was switched on or when an event occurred.

{
“yday” : 351,
“usec” : 969957,
“hour” : 0,
“year” : 2013,
“min” : 6,
“wday” : 3,
“time” : 1387325188,
“sec” : 28,
“month” : 11,
“day” : 18
}

You should take a look at the Squirrel docs for the Date function:

date([time], [format]);

returns a table containing a date/time splitted in the slots:

sec - Seconds after minute (0 - 59).
min - Minutes after hour (0 - 59).
hour - Hours since midnight (0 - 23).
day - Day of month (1 - 31).
month - Month (0 - 11; January = 0).
year - Year (current year).
wday - Day of week (0 - 6; Sunday = 0).
yday - Day of year (0 - 365; January 1 = 0).

if time is omitted the current time is used.
if format can be ‘l’ local time or ‘u’ UTC time, if omitted is defaulted as ‘l’(local time)

I goes wrong when you want to show te date. To follow up on beardedinventor’s post, you can use this as well:
`Months <- [1,2,3,4,5,6,7,8,910,11,12];

server.log(Months[date.month]);
`

or

d = date(time() + (timezone*60)); // get the date in your timezone datestring = format("%04d%02d%02d-%02d:%02d:%02d", d.year, d.month+1, d.day, d.hour, d.min, d.sec); //e.g. 20130511-00:57:25

`

Thanks Matt and DolfTraanberg, If it was a Sunday when I tested this I may have worked it out…

if you want to show the time, it’s easier to get it from Google. Then you have Daylight Savings correction as well.
Something like this:
`
local SENSOR = false;
local coordinates = (latitude + “,” + longitude);
local timestamp = (time()).tostring();

response = http.get(“https://maps.googleapis.com/maps/api/timezone/json?location=”+ coordinates +"&timestamp="+ timestamp +"&sensor="+ SENSOR).sendasync(function(response) {
if (response.statuscode = 200) {
local decodedJSON = http.jsondecode(response.body);
//server.log("maps.googleapis.com response.body: "+ response.body);
DaylightOffset = (decodedJSON.dstOffset.tointeger()); // daylightsavings correction
TimeZoneOffset = (decodedJSON.rawOffset.tointeger()); // timezone correction
TotalTimeOffset = (DaylightOffset + TimeZoneOffset); // timezone and daylightsavings correction
server.log("Total time offset: " + TotalTimeOffset);
TimeStatus = (decodedJSON.status);
}else{
server.log("Error getting timeOffset. code: "+ response.statuscode);
}
}`

Anyone else got it working accurate to milliseconds?

Sure, you can do that.
This code is only ment to get the time offset ( timezone and Daylight Savings) based on location (and a lot of other data).

Yes, but it can be off by up to a minute some days.

you just get your GMT the way you are used to. Then you add the time offset to it.
DLS changes only two times a year and timezone changes only when you move to different timezones

DolfTraanberg, I am trying to use your code for getting the time offset that you posted in December 2013. However, newby that I am, I cannot get it to work in the agent code I am using. When I paste in your code the compiler complains that the first line after your code (which was fine before) is now in error. What am I missing?