Python Forum

Full Version: getting nanoseconds in linux
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
last night i was trying to quickly recall what syscall i used in C on Linux to get the time with nanoseconds.  i thought it was "clock"-something or "get_clock"-something.  so i "cheated" and ran a command under the strace command to see the syscalls it does.  i ran the date command with the "+%N" option to ensure it would get nanoseconds since this option would display (only) the nanoseconds.  to my surprise, i could not find a "clock" syscall at all.  for those of you running linux and doing cli,  see if you can see what syscall is used to get the current time with microseconds.

strace date +%Y-%m-%dT%H:%M:%S.%N
The function is clock_gettime

Output:
strace date +%Y-%m-%dT%H:%M:%S.%N 2>&1 | grep clock clock_gettime(CLOCK_REALTIME, {1506547158, 42515000}) = 0
URL: http://pubs.opengroup.org/onlinepubs/790...ttime.html
that's what the date command should be doing, but it didn't do that, yet it still got nanoseconds.  clock_gettime is not implemented in the os module (or sys).  so i was thinking that whatever way the date command was doing it might be doable in python.
Without using a c-api or an external module, it looks like the only thing available in python is also only sometimes available on Solaris: https://docs.python.org/3/library/time.h...CK_HIGHRES

And based on the wording... nanosecond accuracy might not be available on every platform/architecture.