Tracking periodicity of events

I want to be able to determine the periodicity of events at agent level when triggered by a device.

I was planning to create a global variable “eventdate” which would then get updated by the squirral date() function. However I am struggling to find relevant information about how this function works (new to imp and documentation not structured enough to get answers simply and search function on forum more often than not does not throw up anything obvious).

For debugging purposes I simply assigned “eventdate” with date(), but what threw me was when I printed out the eventdate on server log, it gave me… Last Event Received: (table : 0x7f4e7965a480)

What is table: 0x7f4… ? as was expecting a date in UTC format. Maybe it is the way I am printing out date as text string.

Then how does calculating date differences work. Is it safe to assume its simple subtraction, so that when new event pops up I can simply subtract new date() from last recorded eventdate.

The month+1 is how Squirrel represents months in the date() object. This is likely so that you can have the following code:

months = ["Jan", "Feb", "Mar", "Arp", //... local month = months[date().month];

The result of date() is always a table, a “broken-down time”, with separate fields for year, month, date etc., as you can see by doing this:
server.log(http.jsonencode(date()));
The fields you want to use to calculate date differences are “time” (Unix time_t in seconds since 1970, whole seconds) and “usec” (fractional part of time_t in integer microseconds).

Peter

Aha now that works. Not obvious but then still on the learning curve grappling with the code syntax. Say is there any simple / clever way to parse the jsonencode(date()) into a handy data table and then use this to create a nice easy to read date and time string (as in dd/mm/yyyy hh:mm:ss or in similar format to the auto log as seen through IDE).

Similarly with using time() function to calculate date/time difference. Is there a nice handy way to get this nicely formatted into a easy to read date/time string.

I notice in squirrel code library reference they note date() function as date([time], [format]); where if [time] omitted it is current time and if [format] is omitted it is local time. For “format” you have two flags ‘l’ for local and ‘u’ for UTC. I was then trying to get the correct UTC+1 time as automattically logged for every log update but the date() is only giving me UTC time regardless, as in 1 hour behind. How do I correctly format to give UTC+1.

local timezone = 1; local d = date(time() + (timezone*60*60)); // get the date in your timezone local datestring = format("%04d/%02d/%02d - %02d:%02d:%02d", d.year, d.month+1, d.day, d.hour, d.min, d.sec); //e.g. 2014/06/12 - 14:07:26 server.log(datestring);

this code has no Daylight Savings correction

Thanks your example really helped. This helped me understand the squirrel library documentation for date() function as I could now do the following to get an easy to read format and UTC+1 (although as you say this does not automatically adjust to daylight savings):

`local aDate = date(); // note using ‘u’ or ‘l’ as optional format did not work for me… as formatted Date as shown below or jsonecoded printout option (as above) came out as 1970/00/01 - 01:01:48

nice2readDate = format("%04d/%02d/%02d - %02d:%02d:%02d", aDate.year, aDate.month, aDate.day, aDate.hour + 1, aDate.min, aDate.sec);
server.log(nice2readDate);`

Just to add I had not spotted that one needs to also add +1 to month otherwise give month before which is curious, as in:

nice2readDate = format("%04d/%02d/%02d - %02d:%02d:%02d", aDate.year, aDate.month+1, aDate.day, aDate.hour + 1, aDate.min, aDate.sec);