11

Is it possible in cocoa touch to get the local timezone of device based on the coordinates from the gps? I saw this asked last year, but no answer...

All I really want is the number of hours difference from GMT, and I don't want to query a web service.

Or, can I ask the device what timezone it is set to?

Can it be done?

Steve
  • 6,332
  • 11
  • 41
  • 53
  • If you are determined to figure out the timezone based on latitude and longitude *independent of the system*, please read my answer here: [Figure out time by latitude/longitude?](http://stackoverflow.com/questions/4632100/figure-out-time-by-latitude-longitude/11085220#11085220) – mpemburn Jun 25 '12 at 11:47
  • See also: [How to get a time zone from a location using latitude and longitude coordinates?](http://stackoverflow.com/a/16086964/634824) – Matt Johnson-Pint Jul 05 '14 at 22:54

2 Answers2

23

Use [NSTimeZone localTimeZone];

Edit: The returned object has a -(NSInteger)secondsFromGMT method you can use to figure out the difference from GMT.

James J
  • 6,428
  • 6
  • 35
  • 45
  • 1
    This appears to get its value from the `defaultTimeZone` for the application, and if it's not set explicitly, then from the `systemTimeZone`. Is there a way to get the timezone from the latitude and longitude? – Steve Dec 07 '10 at 20:07
  • Not built into the system. You'd have to build something yourself, perhaps use the data files at http://www.date-time-zone.com/ and compare the user's coordinates against the time zone definitions contained therein. – James J Dec 07 '10 at 20:13
  • Thanks, @jenningj. After looking at this some more, I realize how hard it is to do. Looks like there are web services to do this, but I see why most apps just ask you to tell them the timezone, or get it from a city list. – Steve Dec 08 '10 at 15:41
2

It is not actually possible, unless date is actually included in the input. Why? Because the offset changes with time. Most commonly, there is the switch to daylight saving time in North America and summer time in Europe. Various locations make the switch at different times. Additionally, the formulae used to determine when to make the switch change over time. A few years ago in the US, for example, daylight saving time was extended. Then you have places like Guatemala that try out daylight saving time some times and then decide next year not to do it.

Bottom line, there is not a one to one relationship between GPS coordinates and timezone. There is a one to one relationship between coordinates + date and timezone, though. But it's a moving target.

Victor Engel
  • 2,037
  • 2
  • 25
  • 46