How to get a time zone from a location using latitude and longitude coordinates?
Time Zone Location Web Services
- Google Maps Time Zone API
- Bing Maps Time Zone API
- Azure Maps Time Zone API
- GeoNames Time Zone API
- TimeZoneDB API
- AskGeo - commercial (but arguably more accurate than GeoNames)
- GeoGarage Time Zone API - commercial, focusing on Nautical time zones.
Raw Time Zone Boundary Data
- Timezone Boundary Builder - builds time zone shapefiles from OpenStreetMaps map data. Includes territorial waters near coastlines.
The following projects have previously been sources of time zone boundary data, but are no longer actively maintained.
- tz_world - the original shapefile data from Eric Muller
- whereonearth-timezone - GeoJSON version with WOEDB data merged in
Time Zone Geolocation Offline Implementations
Implementations that use the Timezone Boundary Builder data
- node-geo-tz - JavaScript library (Node.js only)
- timespace - JavaScript library
- tz-lookup-oss - JavaScript library
- GeoTimeZone - .NET library
- Geo-Timezone - PHP library
- timezonefinder - Python library
- ZoneDetect - C library
- Timeshape - Java library
- TimeZoneMap - Java and Android library
- lutz - R library
- go-tz - Go library
- Timezone lookup - Go library
- docker-timezone-lookup - docker container wrapping node-geo-tz
Implementations that use the older tz_world data
- latlong - Go library (Read this post also.)
- TimeZoneMapper - Java library
- tzwhere - JavaScript/Node library
- pytzwhere - Python library
- timezone_finder - Ruby library
- LatLongToTimeZone - Java and Swift libraries
- What Time is it here? - Blog post describing PHP and MongoDB
- rundel/timezone - R library
Libraries that call one of the web services
- timezone - Ruby gem that calls GeoNames
- AskGeo has its own libraries for calling from Java or .Net
- GeoNames has client libraries for just about everything
Self-hosted web services
- geo2tz - based on Timezone lookup, available via Docker image
Other Ideas
- Find the nearest city with an R-Tree
- Find the nearest city with MySQL
Please update this list if you know of any others
Also, note that the nearest-city approach may not yield the "correct" result, just an approximation.
Conversion To Windows Zones
Most of the methods listed will return an IANA time zone id. If you need to convert to a Windows time zone for use with the TimeZoneInfo
class in .NET, use the TimeZoneConverter library.
Don't use zone.tab
The tz database includes a file called zone.tab
. This file is primarily used to present a list of time zones for a user to pick from. It includes the latitude and longitude coordinates for the point of reference for each time zone. This allows a map to be created highlighting these points. For example, see the interactive map shown on the moment-timezone home page.
While it may be tempting to use this data to resolve the time zone from a latitude and longitude coordinates, consider that these are points - not boundaries. The best one could do would be to determine the closest point, which in many cases will not be the correct point.
Consider the following example:
The two squares represent different time zones, where the black dot in each square is the reference location, such as what can be found in zone.tab. The blue dot represents the location we are attempting to find a time zone for. Clearly, this location is within the orange zone on the left, but if we just look at closest distance to the reference point, it will resolve to the greenish zone on the right.
Determine timezone from latitude/longitude without using web services like Geonames.org
I had this problem a while back and did exactly what adam suggested:
- Download the database of cities from geonames.org
- convert it to a compact lat/lon -> timezone list
- use an R-Tree implementation to efficiently lookup the nearest city (or rather, its timezone) to a given coordinate
IIRC it took less than 1 second to populate the R-Tree, and it could then perform thousands of lookups per second (both on a 5 year old PC).
Timezone from geolocation
A website called EarthTools has a web service for that.
http://www.earthtools.org/webservices.htm#timezone
You can pass in latitude and longitude like so:
h ttp://www.earthtools.org/timezone/40.71417/-74.00639
And it spits out the time zone and a bunch of other info:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<timezone xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.earthtools.org/timezone.xsd">
<version>1.1</version>
<location>
<latitude>40.71417</latitude>
<longitude>-74.00639</longitude>
</location>
<offset>-5</offset>
<suffix>R</suffix>
<localtime>4 Dec 2005 12:06:56</localtime>
<isotime>2005-12-04 12:06:56 -0500</isotime>
<utctime>2005-12-04 17:06:56</utctime>
<dst>False</dst>
</timezone>
Get PHP Timezone Name from Latitude and Longitude?
Geonames should do the job nicely:
http://www.geonames.org/
They've also got a php library.
Getting Time Zone from Lat Long Coordinates?
This works as expected:
import geonames
geonames_client = geonames.GeonamesClient('demo')
geonames_result = geonames_client.find_timezone({'lat': 48.871236, 'lng': 2.77928})
print geonames_result['timezoneId']
Output:
'Europe/Paris'
JavaScript's time zones as a way to locate a user's country
After some research, turns out it depends on the scenario (was predictable). For my scenarios, American get the right Timezone 95%+ of the time, while Canadian get it 90%+.
The way to test this out was to save the user's JS timezone in our Google Analytics, and then compare it to their location.
So your mileage may vary, but for my use case, this was more than enough.
Related Topics
How to Access Nested Elements of a Hash with a Single String Key
Railstutorial - Chapter 8.4.3 - Test Database Not Clearing After Adding User in Integration Test
Render Three Different Partials Depending on Button Clicked
Rails Article Helper - "A" or "An"
Why Can't I Use an Integer as a Key Using the New Ruby 1.9.2 Hash Syntax
How Does Require Rubygems Help Find Rubygem Files
What Does &: Mean in Ruby, Is It a Block Mixed with a Symbol
Getting Uninitialized Constant Error When Trying to Run Tests
What Is the Ruby Equivalent of Python's Getattr
How to Handle a Thread Issue in Zeromq + Ruby
How to Use Controller Specific Stylesheets in Rails 3.2.1
Rails: Unable to Access Log File