The other answers are probably sufficient in most cases but I thought I'd add my two cents as I ran into a problem on a BusyBox system.
The system in question did not support the %N
format option and doesn't have no Python or Perl interpreter.
After much head scratching, we (thanks Dave!) came up with this:
adjtimex | awk '/(time.tv_sec|time.tv_usec):/ { printf("%06d", $2) }'
It extracts the seconds and microseconds from the output of adjtimex
(normally used to set options for the system clock) and prints them without new lines (so they get glued together). Note that the microseconds field has to be pre-padded with zeros, but this doesn't affect the seconds field which is longer than six digits anyway. From this it should be trivial to convert microseconds to milliseconds.
If you need a trailing new line (maybe because it looks better) then try
adjtimex | awk '/(time.tv_sec|time.tv_usec):/ { printf("%06d", $2) }' && printf "\n"
Also note that this requires adjtimex
and awk
to be available. If not then with BusyBox you can point to them locally with:
ln -s /bin/busybox ./adjtimex
ln -s /bin/busybox ./awk
And then call the above as
./adjtimex | ./awk '/(time.tv_sec|time.tv_usec):/ { printf("%06d", $2) }'
Or of course you could put them in your PATH
EDIT:
The above worked on my BusyBox device. On Ubuntu I tried the same thing and realised that adjtimex
has different versions. On Ubuntu this worked to output the time in seconds with decimal places to microseconds (including a trailing new line)
sudo apt-get install adjtimex
adjtimex -p | awk '/raw time:/ { print $6 }'
I wouldn't do this on Ubuntu though. I would use date +%s%N