If you want to correct the Android clock from the adb command, you can correct it by rooting and typing the following command from the shell.
$ adb shell "su -c 'date `date +%s`'"
However, with this method, you can specify only in seconds, so in the worst case, an error of 1 second will occur. There is no problem in normal use, but this method cannot be used, for example, when the deviation in seconds becomes fatal when analyzing the log output. There is also a method of using the NTP application, but since I found only the application that requires screen operation, I created a script using Python to automate it and corrected it.
Very simply, the adb command is executed at XX hours XX minutes XX seconds .000000.
It seems that it can be done with a shell script, but it was troublesome (I don't know), so I implemented it in Python. Since it is written in 2 series, please change it to 3 series as appropriate.
import sys
import subprocess
import time
from contextlib import closing
m = 8
def loop():
while True:
t = time.time()
it = int(t*10**m)
if it == int(t)*10**m:
subprocess.call("adb shell \"su -c 'date %d'\"" % int(t), shell=True)
break
if __name__=='__main__':
loop()
print "Done"
Call the loop () method and enter an infinite loop until you can set the time. The time is acquired by time.time (), but since the obtained value is float, the value after the decimal point is also included. Compare the value rounded after rounding up to the number of digits specified by m: int (t × $ 10 ^ m $) and the value rounded up and rounded up: int (t) × $ 10 ^ m $, and if they are the same, execute the command. If not, repeat the time acquisition again. In my environment, I was able to set the time without any problem even if I specified up to 8 digits, but it may be better to change this area according to the environment.
I thought it was written, but the method that specifies the precision by giving m as an argument is more beautiful. After that, it may be kinder to be able to time out. I will implement it if I have time.
Actually, there is a delay from the execution of the adb command until it is reflected, so an error of about 10 msec will occur with a margin rather than an 8-digit precision. Therefore, measure how much the delay is (I will post an article about the method later), shift it in advance, and execute the command. In particular
padding = 0.07
...
t = time.time() + padding
The time is staggered in the form of. In my environment, the deviation was about 0.07 = 70msec, and it is not very stable, so this method has to allow a deviation of about 100msec.
Recommended Posts