83

Is there any library (or even better, web service) available which can convert from a latitude/longitude into a time zone?

5
  • 9
    Is there a non-webservice solution for this? I have a use-case where I have sporadic internet connectivity and still need to get this information in the field. A database of timezone geography with an API on top would be useful. Jan 10, 2011 at 14:12
  • Simon, see: stackoverflow.com/questions/5584602/…
    – fjsj
    Oct 18, 2011 at 13:41
  • Yes, there is a commercially available library available at askgeo.com for doing exactly this. It is written in Java, is thread safe, and very fast (10,000 queries per second).
    – James D
    May 8, 2012 at 21:27
  • 1
    When all is said and done, time is just a dimension for scheduling. "Hours" are not relative to the local diurnal cycle since this varies annually even for a given location. It would make things immensely simpler if everyone used Sidereal time in seconds, kiloseconds, megaseconds etc. A kSec is about 17 minutes which is a very human chunk of time. A day is roughly 86.4kSec. It's not an even number, but so what? No day is ever exactly the same length as the one before because the spinning of the planet is slowing.
    – Peter Wone
    May 30, 2012 at 0:45

8 Answers 8

65

I looked fairly deeply into this question for a project I am working on. GeoNames.org and EarthTools.com are both good options for many situations but with the following serious flaws:

  • GeoNames.org finds the time zone by searching for the nearest point in their database that contains a time zone field. This often leads to the wrong result near borders. It is also painfully slow, leading to query times on the order of a couple seconds per request. It also doesn't return a valid time zone if there is no item in their database near the query point. GeoNames also restricts the number of queries that can be made per day, making bulk operations difficult.
  • EarthTools.org uses a map and is able to return queries quickly, but it doesn't take into account daylight savings time for most locations, and it returns a raw offset rather than a time zone ID (i.e., they return "GMT-7" instead of "America/Chicago"). Also, I just looked at their page while preparing this post and Google Chrome warned about malware on their site. That is new to me and it may change, but is obviously a cause for concern.

These flaws meant that these existing tools were not suitable for my needs so I rolled my own solution and have published it for general use. You can find it here:

http://www.askgeo.com/

AskGeo is based on a time zone map of the world, so it returns a valid time zone for every valid latitude and longitude. It returns the standard time zone ID (e.g., "America/Los_Angeles") used on Linux and most other operating systems and programming frameworks. It also returns the current offset, taking full account of daylight savings time.

It is extremely easy to use and usage is documented on the main page of the site. The API supports batch queries, so if you need to do a lot of look-ups, please use the batch interface rather than bog down our servers with serial requests. The bulk queries are also much faster, so everybody wins.

When we first launched this, we built it on Google App Engine (GAE) and made it free to all users. This was possible because GAE's prices were so low at that time. Since then, our server load has increased substantially and GAE's prices went way up. Both factors combined led us to switch to Amazon Web Services for hosting and to start charging for commercial use, while keeping the service free for non-profit, non-commercial open source projects, and researchers. For commercial users, we provide 1000 free queries to let potential customers evaluate the API to make sure it meets their needs. See the web site for pricing and terms.

The underlying library was written in Java and due to popular demand, we also released the library under a commercial license. Full documentation of the library and pricing details are on the web site.

I hope this is useful. It certainly was useful for the project I was working on.

9
  • Is there a way to convert from your time zone ID (e.g. "America/Los_Angeles") to a .NET System.TimeZoneInfo? May 16, 2011 at 18:25
  • This page contains the mapping between the tz database ids (also known as Olson ids) and the Windows timezone ids: unicode.org/repos/cldr-tmp/trunk/diff/supplemental/…
    – James D
    May 17, 2011 at 19:23
  • Nice work. It would be good if a date could be passed in to get the daylight adjusted offset for a different point in time. Adding the Windows TimeZone Name would be an added bonus.
    – CodeGrue
    Aug 16, 2011 at 13:13
  • 1
    Do you plan to accept a callback parameter so that JSONP would work? Aug 30, 2011 at 10:15
  • 8
    Unless I missed something this is no longer free:The API is free to try, and it is free for non-commercial and personal use. Open source projects, students, and private individuals may use it free of charge (subject to our usage guidelines). Any user that wishes to use the API for commercial purposes must pay an access fee of $200 per year.
    – Arry
    Apr 5, 2012 at 10:53
28

Take a look at Geonames.org

It's a free webservice that allow you to get a lot of informations from a long/lat

They also provide a free (and open source) Java Client for GeoNames Webservices library (library for other language also provided: ruby, python, perl, lisp...)

Here's some info you can get from long/lat: (complete list of webservices here)

  • Find nearest Address
  • Find nearest Intersection
  • Find nearby Streets
  • Elevation
  • Timezone
2
  • 1
    this will give you the current time and the offsets for daylight savings and standard but I'm having trouble finding whether a date in the future is under Daylight savings Nov 5, 2008 at 14:24
  • for that, the Java Calendar will probably be more usefull Oct 8, 2009 at 14:30
12

Timezones are now available via Google API

https://developers.google.com/maps/documentation/timezone/

1
  • This is great, do you know if I'm using Google Maps API for geocoding as well, if the 2,500 limit is for all requests or if its 2,500 for geocoding and another 2,500 for time zone?
    – Tesla
    Oct 20, 2012 at 14:01
8

The Yahoo places API provides timezone information via reverse geolocation.

Check it out.

http://developer.yahoo.com/geo/placefinder/guide/requests.html

2
  • For PHP I use unserialize(file_get_contents()) on the base URL with location=lat%20long&gflags=R&flags=PT to get an array which includes timezone.
    – Owen
    Apr 17, 2012 at 10:40
  • 1
    The query url looks like this: where.yahooapis.com/… Aug 29, 2012 at 7:28
5

Eric Muller has made shapefile maps for the timezones of the tz (Olson) database. A few minor caveats, though:

  1. The boundaries used are often unofficial.
  2. It isn't updated as regularly as the tz database itself, so some newly-formed or -adjusted zones may be missing.

Those aside, however, it seems to be very accurate for most purposes.

4

How much accuracy do you need? Dividing the longitude by 15 would almost be right :p

4
  • 6
    I was being pretty facetious when I made this answer, but not until I came across this question again when trying to solve the problem myself did I realise how totally useless the idea is: upload.wikimedia.org/wikipedia/commons/e/e7/Timezones2008.png (unless you're in international waters)
    – Tom
    Jun 3, 2009 at 11:41
  • 1
    I look at that map and think the approach you suggested is actually pretty decent for a rough estimate. I, for example, need something pretty approximate (a couple of hours out is fine), and dividing the longitude by 15 looks like a good solution. It appears from the map that there are very few inhabited places on earth, around the international date line, where dividing by 15 is going to get it horribly wrong.
    – MB.
    Dec 16, 2010 at 9:23
  • 2
    Getting the timezone in from a political point of view using math is not always recommended. But if one needs to know if there is night or day, this approach can be solved programatically without any webservice. Thanks for this idea.
    – JackPearse
    May 25, 2011 at 8:45
  • Yes, askgeo.com (full disclosure: I run the site). You can buy a Java library that does this on your own server.
    – James D
    May 8, 2012 at 20:54
4

Project dead :-/

These look pretty promising-

Archive link:

https://web.archive.org/web/20150503145203/http://www.earthtools.org/webservices.htm

2
  • 1
    They only provide a raw offset from GMT, not a time zone ID, and they don't take into consideration DST in most of the world, which makes it pretty unusable in my opinion.
    – James D
    May 8, 2012 at 16:37
  • They seem to be gone
    – ligi
    Sep 12, 2017 at 16:00
0

DRT Engine takes a latitude, longitude and local datetime and returns a timezone offset. This can be used to establish the timezone of a particular location at a future date.

Not the answer you're looking for? Browse other questions tagged or ask your own question.