SOLVED: Datetime accuracy in javascript only milliseconds

If I call

console.log(time.ZonedDateTime.now().nano());

in my Rule, I get something like

867000000

which are 867.000000 milliseconds, without micro and nano part

Is there a way to increase the accuracy to nanoseconds? I need it to measure runtimes of some function blocks.

btw., I also tried something like

performance.now or process.hrtime

and the underlying container is supporting high resolution ticks. I confirmed it with

date +%s%6N

.nano() returns the nanos of second field of the ZonedDateTime, so this might be correct (not sure about that).

In case you want to get the milliseconds since that date in 1970, use time.Instant.nano().

And if millisecond accuracy is enough, you could use console.time() and console.timeEnd() to measure execution times: console: time() static method - Web APIs | MDN.

Thanks for your reply.

time.Instant.nano is undefined.

and time.ZonedDateTime.now().nano() gives me the nano seconds but the last 6 digits are always zero. This means the accuracy here is only milliseconds.

and milliseconds are not enough, because most measured cases are <1ms. Means 0.1ms or 0.6ms etc (numbers are from my current python implementation)

process.hrtime is NodeJS stuff, I don’t know whether GraalJS has implemented that, but since you say it does not work, I guess it is not supported.

I have checked what JS-Joda and the java.time package provide, I would try it with the following:

var System = Java.type('java.lang.System');

var start = System.nanoTime();
// do some stuff
var end = System.nanoTime();
var duration = end - start;
console.log('Took nanos: ' + duration);

Please note that there are some “warm-up” times, so execution is faster on subsequent runs than on the first run.

1 Like

Thanks,

this works for me!

1 Like