Is there anyway to tell which timezone/country the phone is located in? I would prefer not to use location services.
The purpose of this is to automatically route calls. If the user is in the US, I want the app to default to calling the US based call center. If the user is in another country, I want it to default to their call center.
Thanks!
UTC Date Format Use DateFormatter to parse and format a date in Swift. The default time zone for a DateFormatter is the device's local time, so to obtain UTC time the timeZone value needs to be configured. let utcDateFormatter = DateFormatter() utcDateFormatter. dateStyle = . medium utcDateFormatter.
You can get the name of the local time zone using:
NSTimeZone *timeZone = [NSTimeZone localTimeZone]; NSString *tzName = [timeZone name];
The name will be something like Australia/Sydney
, or Europe/Zurich
, or America/Denver
. Since it sounds like you might only care about the continent, that might be all you need.
If you need more: Get the official time zone database at http://www.iana.org/time-zones. Download tzdata2013g.tar.gz
(or whatever the most recent version of tzdataYYYYx.tar.gz
is when you're reading this). There's a file in that archive named zone.tab
that gives a lat/long for each time zone, for example:
CH +4723+00832 Europe/Zurich
The +4723+00832
indicates latitude = +47º23", longitude = +8º23".
Update: In some cases (I'm not sure which), the time zone name may be an older-style name like US/Pacific
that isn't listed in zone.tab
. To handle those, also get the file called backward
, which gives translations of older names to current names.
Converting zone.tab
into something your app can use is up to you. The results will be nowhere near as accurate as location services, but will give a general idea. But it's not guaranteed. It will fail in a couple of situations:
The user changes their time zone in Settings. Most users let their phone set the time and zone automatically, but not everyone. If the user changes their time zone, you'll get whatever they want instead of what's accurate.
The device is unable to determine the correct local time zone. Network problems might prevent the phone from setting the time and zone accurately. For example, if the user travels by plane and has network problems when they land, the phone may still show the time zone from their departure location.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With