Tag Archives: Googlу Timezone API

How to get the REAL device time with wrong time zone

The casual approach to time management in Android device is to turn on “Automatic date & time” and “Automatic time zone” in Settings – Date & Time. But what if your user who live in Helsinki (GMT+3) will move to Beijing(GMT+8), unsets both checkboxes, leaves Helsinki’s time zone intact and just adds 5 hours to his current time?

“It’s not a problem you say and will be wrong. When your app will try to get current time using System.currentTimeMillis or Calendar.getInstance().getTimeInMillis you will get 13.00 GMT but not 18.00 GMT as you expected. All the system time functions respect the current device time zone and there is no way to get right global timestamp with wrong time zone.
So where is the solution?
  1. you may get time from Internet
  2. you may use Google Timezone API on client or server side, but the app need to know its location
  3. you can delegate all time routine to server, that will recognize client’s time zone by ip (see Ruby or PHP realizations for example).

But in spite of it all, there are many cases (offline or standalone applications, geodata absence) when the only solution is to ask people to activate time and time zone synchronization.

Advertisements