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?