Usec in date() funcion

Not clearly understand what mean returned number in Agent code. Code are running every 10 seconds.
server.log(date().usec);
2017-10-15 22:28:51 +03:00 [Agent] 189667
2017-10-15 22:29:01 +03:00 [Agent] 208161
2017-10-15 22:29:11 +03:00 [Agent] 237048
2017-10-15 22:29:21 +03:00 [Agent] 252271

It’s a “Unix timestamp microsecond count… If you require microsecond granularity, read the usec key — this gives the microsecond count between each second recorded by time.” (see https://www.electricimp.com/docs/squirrel/system/date/)

Hello! Seems that if I call date() every second I get microseconds between these calls, otherwise - every 10 seconds - microseconds are for first or last second : microseconds in first second after date() or microseconds before date(). That is what I cannot find in documentation.

The usec field is the fractional microseconds of the date structure. If you’re looking to measure time down to microseconds between calls then you should be using date().time (unix time seconds) as the most significant part and date().usec as the least significant.

Hello Hugo, Yes - I just knot test it and now I understand how it works :slight_smile: Thanks!

Anyway - trying code:
local d=date(time());
server.log(format("%d.%06d", d.sec, d.usec) + “_” + d.usec);
I get 0 usec all the time:
2017-10-16 15:27:23 +03:00 [Agent] 23.000000_0
2017-10-16 15:27:25 +03:00 [Agent] 25.000000_0
2017-10-16 15:27:27 +03:00 [Agent] 27.000000_0
2017-10-16 15:27:29 +03:00 [Agent] 29.000000_0
2017-10-16 15:27:31 +03:00 [Agent] 31.000000_0

The usec key is available only when date() is called from within agent code; it is non-functional on the device, where it will always return 0.

Edit: except you are on the agent. Hmmm.

If you call date(time()), then the result is constructed by using the return value of time(), which only has second precision.

This function returns the current date and time in the form of an integer representing the number of seconds that have elapsed since midnight on 1 January 1970.
time() | Dev Center

If you call date(), with no arguments, then the result contains the us field.

server.log(http.jsonencode(date()));
server.log(http.jsonencode(date(time())));
2017-10-16 14:36:55 +01:00	[Agent]	{ "min": 36, "hour": 13, "day": 16, "year": 2017, "wday": 1, "time": 1508161015, "sec": 55, "yday": 288, "usec": 632262, "month": 9 }
2017-10-16 14:36:55 +01:00	[Agent]	{ "min": 36, "hour": 13, "day": 16, "year": 2017, "wday": 1, "time": 1508161015, "sec": 55, "yday": 288, "usec": 0, "month": 9 }

I see but here - why microseconds constantly increasig?
Code:
local d=date();
server.log(format("%d.%06d", d.sec, d.usec) + “_” + d.usec);

Server log:
017-10-16 17:24:58 +03:00 [Agent] 58.486041_486041
2017-10-16 17:25:00 +03:00 [Agent] 0.506774_506774
2017-10-16 17:25:02 +03:00 [Agent] 2.529201_529201
2017-10-16 17:25:04 +03:00 [Agent] 4.569483_569483
2017-10-16 17:25:06 +03:00 [Agent] 6.596966_596966
2017-10-16 17:25:08 +03:00 [Agent] 8.613355_613355

Solved! Thanks to all!

Microseconds are increasing as your callback is obviously being run slightly later each time (by about 30-40ms).