0

I have a mobile application which authenticates the user via a phone number (similar to Whatsapp's sign in process).

After the user has logged in, he can create a TODO notes. When he creates a note he also fills in a date.

In the DB, I want to normalize this date to UTC.
So for example, if the user filled the date: 06/07/2015 17:00:00
And the UTC offset in his country is +3
Then 06/07/2015 14:00:00 will be saved in the DB.

In order to do this, I need to know the UTC offset of the user's country.

When the user registered the only two things he provided are:

  • country code
  • phone number

Those are not enough to know the UTC offset, because a user who entered a country code of US can still be in several different time zones.

I am also using libphonenumber to parse the incoming values mentioned above, but it can't get me the offset I want.

How can this be solved ? (How other apps like Viber, Line and Whatsapp handle this scenario?)

Yaron Levi
  • 12,535
  • 16
  • 69
  • 118
  • Why can't you just get the time zone from the device itself? – Adrian Wragg Jul 06 '15 at 15:18
  • You can't, especially with mobile numbers. Mobile numbers are *not* assigned geographically. Besides, number portability means that you can't even detect the carrier by the number – Panagiotis Kanavos Jul 06 '15 at 15:20
  • Mobile applications use the device's time zone information and/or location. They don't try to guess from the number. – Panagiotis Kanavos Jul 06 '15 at 15:21
  • So I need to get the time zone from the device (use some api in iOS and Android) and send it to server in the authentication process? – Yaron Levi Jul 06 '15 at 15:23
  • Do you now know why a lot of apps ask for your position? :-) – flindeberg Jul 06 '15 at 15:28
  • @flindeberg So the timezone comes from reading GPS input? or is it coming from an operating system api call that access the sim/carrier? (I don't want to enable GPS) – Yaron Levi Jul 06 '15 at 15:34
  • With a rough estimate of your position getting timezone is trivial. It has nothing to do with GPS specifically, only positioning. Although one problem remains, is it the _relevant_ timezone for the user? I would rather try to access the device settings. – flindeberg Jul 06 '15 at 15:40
  • GPS would give location; location to timezone mapping isn't a simple process. Your best bet is to find the individual API call that gives you timezone data - http://stackoverflow.com/questions/19186666/get-timezone-country-from-iphone gives a few pointers. – Adrian Wragg Jul 06 '15 at 15:40
  • 1
    @AdrianWragg I would say location to timezone is fairly simple, one webservice call, see http://stackoverflow.com/questions/16086962/how-to-get-a-time-zone-from-a-location-using-latitude-and-longitude-coordinates for a suitable list. – flindeberg Jul 06 '15 at 16:04
  • 2
    If you have and app - I don't think there is a language that *does not* support converting time to UTC. Performing local->UTC on the client is the best option you have (and no need for getting time zone, which will be additional pain to work with when you get it to server). – Alexei Levenkov Jul 06 '15 at 16:47
  • Related: http://stackoverflow.com/a/15414820/634824 – Matt Johnson-Pint Jul 06 '15 at 17:35

2 Answers2

1

I'd like to rephrase your question into this:

How can I get the timezone from a phone number?

And the answer is, no, you cannot get the timezone from only a phone number (including country code).

What I would do is to have a look at the client sending you either a full DateTimeOffset, which includes timezone, or a DateTime with some kind of "position" and you try to elicit it yourself on the server.

flindeberg
  • 4,887
  • 1
  • 24
  • 37
0

As Alexi said, the best thing to do here is to never handle the UTC conversion in the server side. Instead, the client will send the date already in UTC. The conversion to/from UTC will always be done client side, according to the time offset defined in the OS (meaning the locale). The server is only gets UTC dates from the client.

Yaron Levi
  • 12,535
  • 16
  • 69
  • 118