Figure out time by latitude/longitude?

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?

  • Date to milliseconds and back to date in Swift
  • Swift: how to create a date time stamp and format as ISO 8601, RFC 3339, UTC time zone?
  • NSTime create a timeInterval
  • iOS Format String into minutes and seconds
  • Objective C: Get notifications about a user's idle state
  • Smart Formatting of time span
  • Thanks in advance.

    5 Solutions Collect From Internet About “Figure out time by latitude/longitude?”

    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.

    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.

    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.

    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

    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