1

Here's the deal. I have a latitude/longitude set of a location. I need to figure out what the current time is in that location. Here's how I was getting it before:

NSDateFormatter *theFormatter = [[NSDateFormatter alloc] init];
[theFormatter setDateFormat:@"h:mm a"];
NSDate *todaysdate = [NSDate date];
NSString *todaysDate = [theFormatter stringFromDate:todaysdate];
[theFormatter release];

However, I realized that this will give the time for the user's current location. Is there an API somewhere that gives me the time based off of a lat/lon pair?

Thanks in advance.

sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
  • 1
    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

5 Answers5

2

Unfortunately timezones (and time in general) is never as simple as you would like.

For a simple approximation you can follow jer's suggestion. Longitude ranges from -180 to 180 degrees, there are 24 hours in a day, so you get 15 degrees of longitude per time zone. Center those time zones on 0 degrees longitude so UTC extends from -7.5 to 7.5, UTC+1 is from 7.5 to 22.5, UTC-1 is from -7.5 to -22.5, and so on. You would then have a very simplistic, and wrong, model of how we use time zones.

Take a look at this map of time zones.

  • Time zones are not well ordered; regions in UTC-1 are adjacent to regions in UTC-3.
  • UTC-9.5, UTC-4.5, UTC+3.5, UTC+5.75, and UTC+13 are all valid time zones and actively in use.

Once you get that sorted out then you can start to consider daylight savings time.

Jonah
  • 17,918
  • 1
  • 43
  • 70
  • Thanks for your detailed answer. I think this is too complicated and messy to try to figure it out on my own. I'll just use WeatherBug's API, which gives me the time (and other info) of the lat/lon pair. It requires n/w access, but my app requires it anyway, so it won't make too much of a difference. – sudo rm -rf Jan 09 '11 at 20:58
  • 1
    You can also use [AskGeo](http://www.askgeo.com/) (objC bindings https://gist.github.com/907603). Cheers! – jbenet Jul 22 '11 at 12:45
1

This is not a trivial problem -- but it's not one without a solution. Services like Geonames and WeatherBug are very restrictive and/or costly so don't start developing something before you thoroughly understand their terms of service.

Here are the steps that I followed for my Appcelerator-based iPhone app:

  1. Locate a list of coordinates that describe all timezones as polygons and read them into a SQLite table.
  2. Make a best guess of which timezone the user is in (or is seeking), based on the longitude. This will be several zones.
  3. Pull the polygons of each of the best-guess timezones from the database and run each one through a find-point-in-polygon algorithm. The one that returns true is the correct timezone.

If you a not concerned about Daylight Saving Time, you're done, otherwise, you have a mess to deal with. The landscape of DST is completely illogical and changes frequently. The best I could do was to Google "Daylight saving time rules" (start looking here: Sources for Time Zone and Daylight Saving Time Data) and be prepared to do a lot of work setting up list that you can use for the calculation. Mine still does not work perfectly after a lot of tweaking.

mpemburn
  • 2,776
  • 1
  • 35
  • 41
1

No, you will have to write one. Since the latitude and longitude points are well known, and timezones are also well known (though change periodically), all you have to do is map where the lat/long boxes are in given timezones, and calculate if your current position is in which one of those boxes. Once you know which box you're in, you'll have enough info to figure out what your time is.

jer
  • 20,094
  • 5
  • 45
  • 69
  • Thanks for your answer. I found this somewhere on SO: `offset = direction * longitude * 24 / 360`. It makes sense, but I don't know how to use it. Would it be related to `NSTimeZone`? – sudo rm -rf Jan 08 '11 at 03:21
  • 3
    @sudo Just keep in mind that I'm pretty sure Timezones don't obey perfect lines on the globe. They do very bizzare things sometimes, meaning any normal human being looking at a country would assume it's GMT+1 but it could be GMT+2 for whatever reason the "committee" decided. – Andrew T Finnell Jan 08 '11 at 03:24
  • @Andrew Good to know, thanks! For my purposes, I don't think that the it will really matter to be that precise in those questionable time zones. However, I still don't know how to use this offset. :) – sudo rm -rf Jan 08 '11 at 03:41
0

I've written a small Java class to do this, with the data embedded in the code. It could be easily translated to ObjectiveC. The database is embedded in the code itself. It's accurate to 22km.

https://sites.google.com/a/edval.biz/www/mapping-lat-lng-s-to-timezones

Tim Cooper
  • 10,023
  • 5
  • 61
  • 77
0

Use this data dump and import it into your program. Then locate the country in which your lat/long exists and it's time zone. You can then calculate what time it is at a specific lat/lon.

Geonames

Andrew T Finnell
  • 13,417
  • 3
  • 33
  • 49