54

Is there a web service of some sort (or any other way) to pull a current time zone settings for a (US) city. For the parts of the country that don't follow the Daylight Saving Time and basically jump timezones when everyone else is switching summer/winter time... I don't fancy creating own database of the places that don't follow DST. Is there a way to pull this data on demand?

I need this for the database server (not for client workstations) - there entities stored in the database that have City, State as properties. I need know current timezone for these entities at any moment of time.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
sergeb
  • 2,484
  • 3
  • 22
  • 20

8 Answers8

74

We encountered same issue and, alongside the great suggestions above, Google appears to have two complementary APIs, one for Time Zone from geocode (latitude/longitude) data and the geocode API.

For example, to get the time zone and offset for San Francisco:

1) Convert the city to a geocoded location:

http://maps.googleapis.com/maps/api/geocode/json?address=San%20Francisco,+CA&sensor=false

The geocoded location is in the JSON return data:

"location": {
    "lat":  37.77492950,
    "lng": -122.41941550
}

2) Convert the geocoded location to a local timezone and offset, if any:

https://maps.googleapis.com/maps/api/timezone/json?location=37.77492950,-122.41941550&timestamp=1331161200&sensor=false

Which returns the current time zone information:

{
          "status": "OK",
       "dstOffset":  0.0,
       "rawOffset": -28800.0,              
      "timeZoneId": "America/Los_Angeles",
    "timeZoneName": "Pacific Standard Time"
}

Time zones for a region can change for a variety of reasons. So it is a good idea to find an authoritative server-based solution and not cache. For more information see Wikipedia's Time Zone article.

terales
  • 3,116
  • 23
  • 33
Mike Davis
  • 918
  • 6
  • 6
  • 2
    Note, that you can pass non-English city name to the Google Geocode. – terales Apr 07 '14 at 11:04
  • How do you compute current location time with this Google Timezone result ? – Nicolas Thery Jun 08 '16 at 10:43
  • @NicolasThery Without looking at the API docs I'm guessing `rawOffset` is offset against UTC in seconds. So just add this value to a UTC timestamp and you're done. – piit79 Oct 29 '16 at 17:16
  • @NicolasThery Sorry, it looks like you actually have to add `dstOffset` plus `rawOffset` to your UTC timestamp. – piit79 Oct 29 '16 at 17:39
  • how to add a callback function here so it can be used on browser and avoid the cross domain security problem – fekiri malek Dec 22 '16 at 15:04
47

earthtools.org provides a free web service to get the time zone from a city here:

http://www.earthtools.org/webservices.htm#timezone

You just pass in the long/lat values like this: (This is for New York)

http://www.earthtools.org/timezone-1.1/40.71417/-74.00639


EDIT:

It seems like earthtools has been shut down. A good alternative (That did not exist in 2008 when this question was answered) is the Google Time Zone API. To use it you must first activate the Time Zone API on your account. It is free if you stay below these limits:

  • 2500 requests per 24 hour period.
  • 5 requests per second.

The documentation is available on Google Developers.

Espo
  • 41,399
  • 21
  • 132
  • 159
16

Geonames.org has a wonderful set of worldly data that's available via webservice or download:

http://www.geonames.org/export/ws-overview.html

In particular

http://www.geonames.org/export/web-services.html#timezone

.

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
Eric
  • 928
  • 1
  • 7
  • 13
  • However it doesn't allow to specify date,for which you want to find timezone offset – Michael Freidgeim May 13 '12 at 02:39
  • Hi... Thanks for the answer and it helps me a lot. I need to ask you whether daylight saving time is implemented in it? Let me know as i can't test it now? I need current time from lat long. – Sagar Rawal Dec 12 '12 at 10:01
7

Earthtool's timezone info is not up to date ... for an instance, the Sri Lankan current offset is +5.5 from GMT but EarthTools shows as +6 which was the old offset before 2005.

I suggest GeoNames.org.

Steve
  • 50,173
  • 4
  • 32
  • 41
megazoid
  • 1,146
  • 5
  • 17
  • 28
5

WorldTimeServer.com has what appears to be a comprehensive time zone database, which you can purchase access to in a variety of formats, including a .NET component for Web use.

No connection, just had to research the same thing myself recently.

TheoJones
  • 359
  • 3
  • 15
2

Simple Offline Library : APTimeZones

In order to find the time zone for a location you can use such as the Google Maps API’s time zone API. Unfortunately this requires you to query a remote service and you are subject to their limits.

Here’s a library rom Alterplay called APTimeZones(Git is attached) that allows you to extract an NSTimeZone from a given location without the need to connect to a remote service. APTimeZones works by querying a local listing of time zones (included with the library).

Shahid Aslam
  • 2,585
  • 3
  • 24
  • 31
  • 1
    I've rewritten this library in Java, so you can use it on Android devices also: https://github.com/agap/llttz – aga Oct 16 '14 at 13:37
1

In case anyone should bump into this question.

  • You could use Google API to search for an address. That returns latitude / logitude. With those values in hand, you can find the closest timezone using e.g. PHP.

  • Or you can use an API like timezoneapi.io (I'm behind that) which enables you to search for an address / city / country. It returns the address, the timezone information and the current date/time for that given timezone.

https://timezoneapi.io/developers/address

Michael Nilsson
  • 312
  • 2
  • 4
0

I know this is answered, but I am posting this answer as people might still find it useful - The selected answer does not work successfuly right now.

Google have their own service, which is very reliable and easy to use, and outputs info in JSON format. It even allows for specifying a custom time, e.g get the timezone in 02/02/2013 in Malta.

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

Karl Cassar
  • 6,043
  • 10
  • 47
  • 84