9

How can I measure time from adb shell with milliseconds or nanoseconds resolution?

Usingdate +%.%N from adb shell returns 1401546811.N (seconds resolution) instead of something like 1401547289.231869798 (nanoseconds resolution).

How can I get either milliseconds or nanoseconds resolution from adb shell?

Is there some terminal program that I can use to give me this? I am able to measure time using System.currentTimeMillis() and System.nanoTime() from Android application code itself, but I also need something from within adb shell.

Alex P.
  • 30,437
  • 17
  • 118
  • 169
22332112
  • 2,337
  • 5
  • 21
  • 42

3 Answers3

18

mksh (the standard Android shell since version 4.0) has a built-in EPOCHREALTIME environment variable:

Time since the epoch, as returned by gettimeofday(2), formatted as decimal tv_sec followed by a dot . and tv_usec padded to exactly six decimal digits.

So the command to get epoch time with microsecond accuracy in Windows would be:

adb shell echo $EPOCHREALTIME

or in Linux:

adb shell 'echo $EPOCHREALTIME'

If you just need millisecond accuracy:

adb shell 'echo ${EPOCHREALTIME:0:14}'

Or just the milliseconds part to use with another time format:

adb shell 'echo $(date +%T)${EPOCHREALTIME:10:4}'
Alex P.
  • 30,437
  • 17
  • 118
  • 169
  • 1
    why I have no `EPOCHREALTIME` var? which version do I need? I'm using android 4.0.4. Your command echos only text `$EPOCHREALTIME` – zhangxaochen Dec 14 '15 at 02:23
  • Note, if you are on Linux, the SINGLE quotes are very important, otherwise you will get a blank. If you leave them out, your Linux machine will try to expand the $EPOCHREALTIME and it will likely be blank, so adb shell will faithfully echo "" You could also do adb shell "echo \$EPOCHREALTIME" where the escaping of the variable is a bit more explicit – KevinL Sep 14 '17 at 14:37
0

Kind of solved my problem by using botbrew. Probably not idea, but at least it works for now. /data/botbrew-basil/init -- date +%s.%N -- returns nanosecond resolution.

22332112
  • 2,337
  • 5
  • 21
  • 42
0

adb shell echo $EPOCHREALTIME gives you the time with micro seconds, (don't write it with '\' before the variable name)

Ortal Turgeman
  • 143
  • 4
  • 14