ntp_gettime
and ntp_adjtime
are both defined in the C library. On Linux and glibc, ntp_adjtime
is just a wrapper for the adjtimex
system call, and ntp_gettime
uses the same syscall by passing it a struct timex
with modes
set to 0. You can browse their definitions at the glibc LXR.
The system call implementation calls _do_adjtimex which calls getnstimeofday to get the time (in nanoseconds) and then hands it to __do_adjtimex. And here is the interesting part :
txc->time.tv_sec = ts->tv_sec;
txc->time.tv_usec = ts->tv_nsec;
if (!(time_status & STA_NANO))
txc->time.tv_usec /= NSEC_PER_USEC;
So if STA_NANO
is set it just puts the nanosecond value in tv_usec
, which is the behavior you've been experiencing. If you want to unset this flag, you can do something like this :
struct timex tmx;
tmx.modes = 0 | ADJ_MICRO;
ntp_adjtime(&tmx);
and then read your microsecond value from tmx.time.tv_usec
. If you want to do this you'll have to run your code with sudo
, but beware that the flag will remain unset in the kernel until you reset it (using ADJ_NANO
) or reboot.