Python Forum
getting nanoseconds in linux
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting nanoseconds in linux
#1
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
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020