79

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

Matt Sheppard
  • 111,039
  • 46
  • 105
  • 128
  • 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. – Simon Gillbee Jan 10 '11 at 14:12
  • Simon, see: http://stackoverflow.com/questions/5584602/determine-timezone-from-latitude-longitude-without-using-web-services-like-geona – fjsj Oct 18 '11 at 13:41
  • Yes, there is a commercially available library available at http://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 08 '12 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 '12 at 00:45
  • See also: [How to get a time zone from a location using latitude and longitude coordinates?](http://stackoverflow.com/q/16086962/634824) – Matt Johnson-Pint May 29 '15 at 17:01

8 Answers8

64

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.

balpha
  • 45,794
  • 14
  • 108
  • 128
James D
  • 1,494
  • 1
  • 17
  • 15
  • Is there a way to convert from your time zone ID (e.g. "America/Los_Angeles") to a .NET System.TimeZoneInfo? – Judah Gabriel Himango May 16 '11 at 18:25
  • This page contains the mapping between the tz database ids (also known as Olson ids) and the Windows timezone ids: http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/zone_tzid.html – James D May 17 '11 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 '11 at 13:13
  • 1
    Do you plan to accept a callback parameter so that JSONP would work? – Jon Nylander Aug 30 '11 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 05 '12 at 10:53
  • We have added the callback parameter, so it now works with JSONP. – James D May 07 '12 at 20:26
  • @Arry - yes that is unfortunate that it is no longer free. 1000 credits flat is ok but it would be better if there was still low-usage free tier that reset daily. – cwd Jun 18 '12 at 20:08
  • I must have missed something but I didn't find a free option. I'm starting an open-source project and I'd like to use the service – chester89 Aug 14 '12 at 13:52
  • @chester89 You can request open source access by email. See "Free Access" section on web page to see what you should include in the email. – James D Sep 27 '12 at 20:44
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
GateKiller
  • 68,419
  • 71
  • 167
  • 203
Frederic Morin
  • 8,183
  • 4
  • 25
  • 26
12

Timezones are now available via Google API

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

john
  • 121
  • 1
  • 2
  • 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 '12 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

Jiho Kang
  • 2,412
  • 1
  • 22
  • 38
  • 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 '12 at 10:40
  • 1
    The query url looks like this: http://where.yahooapis.com/geocode?location=37.775391%20-122.412209&flags=J&gflags=R – Jon Stevens Aug 29 '12 at 07:28
5

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

Tom
  • 36,698
  • 31
  • 90
  • 98
  • 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: http://upload.wikimedia.org/wikipedia/commons/e/e7/Timezones2008.png (unless you're in international waters) – Tom Jun 03 '09 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 '10 at 09: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 '11 at 08:45
  • Yes, http://askgeo.com (full disclosure: I run the site). You can buy a Java library that does this on your own server. – James D May 08 '12 at 20:54
4

Project dead :-/

These look pretty promising-

Archive link:

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

vwvw
  • 321
  • 3
  • 12
chs
  • 125
  • 1
  • 5
  • 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 08 '12 at 16:37
  • They seem to be gone – ligi Sep 12 '17 at 16:00
4

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.

Tim Parenti
  • 501
  • 6
  • 26
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.

Jules
  • 19
  • 2