4

python3 has possibility running following construct:

from datetime import datetime
print({'timestamp':str(int(datetime.utcnow().timestamp() * (10**9)))})

That would output JSON-like structore:

{'timestamp': '1567658264143923968'}

Now I want to get similar result in Node.JS 12.

  • I need a string
  • I need UTC stamp since 1970, but not since machine boot
  • Error no more than last 4 digits (microseconds precision or better)

So 32-bitness of int cant be a limitation. (That cant be a problem in Q3 2019, since Node 10 has bigint support).

OS also cant pose such a limitation, since WinAPI has GetSystemTimePreciseAsFileTime and *nix has clock_gettime(CLOCK_MONOTONIC) both are better than 1us

How could I get nanoseconds UTC timestamp string in NodeJS?


  • Date.now()+"000000" shows that it lacks last 6 digits (millis), but the task was 4 (micros or better)

  • This 7yo question seems to address general timestamp question, and hrtime will get you just machine uptime, not real UTC time. results will be irrelevant with that of python.

What are other options?


UPD

Now, I can reword question a bit. From very simple perspective, Nodejs can be seen as a OS-independent runtime library. You write one js code and it will work quite same on all platforms. So it MUST bind most viable functions to js interface; timer functions are among them. As we saw, monotonic clock QueryPerformanceCounter() call is bound through process.hrtime()

Which function in node.js would lead to a clock_gettime(3) call on linux, or GetSystemTimePreciseAsFileTime() call on windows, and return result to js, with microseconds precision or better?

xakepp35
  • 2,878
  • 7
  • 26
  • 54
  • Have you tried using `process.hrtime()`? https://stackoverflow.com/questions/11725691/how-to-get-a-microtime-in-node-js – Scaccoman Sep 05 '19 at 14:22
  • Alternatively you could use something like: https://www.npmjs.com/package/timestamp-nano – Scaccoman Sep 05 '19 at 14:23
  • @Scaccoman How could you obtain result, that is pronted by python code? `process.hrtime()` will give you invalid result. `Date.now()+"000000"` will lack 6 last digits. – xakepp35 Sep 05 '19 at 14:26
  • I answered to this question here: https://stackoverflow.com/questions/11725691/how-to-get-a-microtime-in-node-js – pirs Apr 14 '20 at 15:49
  • 1
    @pirs I know. If you would pay attention - there were like "search attemt" in the in the question body, referencing almost same link. I may just repeat exactly same words: "This 7yo question seems to address general timestamp question, and hrtime will get you just machine uptime, not real UTC time. results will be irrelevant with that of python." So there is no correct answer yet, and that is a pure node.js issue - they may easily add such support.. – xakepp35 Apr 14 '20 at 16:31
  • @pirs Now, I can reword question: read UPD – xakepp35 Apr 14 '20 at 16:38
  • @xakepp35 I saw the link is already present on the question but i repeated it to be sure, if you test you ll see my post is kind of right answer for many cases, if you want 4digit, you can change it easily with `slice(3,7)` but js `Number()` will round to 3 digits... then, hrTime nanosecond seems to follow the "timestamp ticks" and it fit as well. `clock_gettime(3)` and others OS-based functions seem ok to use, but it not use native code of node.js. – pirs Apr 14 '20 at 16:53
  • @xakepp35 see my update – pirs Apr 14 '20 at 17:04

3 Answers3

0

My temporary solution:

let startNs = process.hrtime();
let startMs = new Date().getTime();
function nanoseconds() {
  let diffNs = process.hrtime(startNs);
  let endMs = new Date().getTime();
  if (Math.abs(diffNs[0] * 1000 - (endMs - startMs)) > 3000) {
    startNs = process.hrtime();
    startMs = new Date().getTime();
    diffNs = process.hrtime(startNs);
  }
  let bi= BigInt(startMs) * 1000000n +  BigInt(diffNs[0]) * 1000000000n + BigInt(diffNs[1]);
  return bi.toString();
}
-1

If the resolution you are looking for is microseconds then process.hrtime() should be enough.

const hrTime = process.hrtime()
console.log(hrTime[0] * 1000000 + hrTime[1] / 1000)

Alternatively you could use timestamp-nano

If you need an even higher precision you could build yourself a native node module: https://medium.com/the-node-js-collection/n-api-next-generation-node-js-apis-for-native-modules-169af5235b06

Or if you really like Python, you could execute a python script from Node: How to call a Python function from Node.js

Scaccoman
  • 455
  • 7
  • 16
  • Tried, result is different from python's output – xakepp35 Sep 05 '19 at 15:44
  • That's interesting, I guess that Python and Node.js use different systems to calculate the timestamp. What about calling the Python script from Node? Does that produce a correct timestamp? – Scaccoman Sep 05 '19 at 15:50
  • 1
    `hrtime` uses like realtime counter (`QueryPerformanceCounter` on windows), that timer is started when cpu is initialized. On contrast, normal implementation should call `clock_gettime(3)` on linux, and `GetSystemTimePreciseAsFileTime` on windows, to get absolute time. Calling python from node is absolute nonsense.. I've just pointed out that, it seems that corresponding function was not implemented in node. As always I may write C plugin, using V8 api, but.. that all is a stub, that has to be manually supported and may not work on some systems – xakepp35 Sep 16 '19 at 15:08
-1

For recent versions of Node, process.hrtime.bigint() seems to provide this.

Devoops
  • 2,018
  • 8
  • 21
  • This does NOT provide UTC nanosecods! I quote the node docs: `These times are relative to an arbitrary time in the past, and not related to the time of day and therefore not subject to clock drift. The primary use is for measuring performance between intervals:` – user3413723 Jan 25 '23 at 19:13