Search code examples
androidlinuxshelladb

How to synchronize date, time and time zone between an android tablet and a Linux system?


I am working with a Linux system that is connected to an Android tablet. I need to synchronize the date, time and timezone. I have software running on both and would like to correlate logcat messages from the Android tablet with log messages on the Linux system. Both of which include a time stamp.

I have found that I can synchronize the date and time using this command.

adb shell date $(date +%s)

That does not set the TimeZone however.

I can get a numeric time zone using this command

date +%z

The software on my Linux system sets this offset using a list of UTC offsets (UTC-0700, UTC+0700, etc.) and a checkbox indicating if daylight savings is being observed. If it is the offset is incremented by an hour.

Update: The software on my linux system compiles its own /etc/localtime file which contains the UTC offset and some binary data.

$ ll /etc/localtime
-rw-r--r--. 1 root root 120 Jan 18 14:06 /etc/localtime
$ date
Wed Jan 23 13:05:37 -07 2019

Android requires a Region/City.

adb shell setprop persist.sys.timezone "America/Denver"

If I understand correctly this allows android to set the UTC offset and take into account this timezone's observation of daylight savings time.

For the time being I have created a switch statement in my shell script that maps each specified offset to a Region/City. This is problematic as some of the offsets provided by the software on my linux system don't exist in the list of time zones found here

adb shell date $(date +%s)
timeZoneOffset=`date +%z`
case "$timeZoneOffset" in
    +1400)
        timeZone="Pacific/Kiritimati"
        ;;
...
    -1200)
        timeZone="Etc/GMT+12"
        ;;
    *)
        timeZone="Europe/London"
        ;;
esac
adb shell setprop persist.sys.timezone $timeZone

This script is run at startup and a restart is required when the software on the Linux system changes the timezone. This works most of the time unless I picked a Region/City that observes (and happens to be observing) daylight savings time or no Region/City matches the offset I am given.


Solution

  • Generally, the time zone on any *nix system is set by creating a symlink /etc/localtime to one of the zoneinfo files in /usr/share/zoneinfo/. The files in zoneinfo are generally named things like Europe/London, so the command "readlink /etc/localtime | cut -d/ -f5-" would probably give you the correct timezone value unless something is handled differently on your system. If that is the case, you may need to make/find some utility that can match up the selected timezone with the closest entry in the zoneinfo database.