Skip to content

Instantly share code, notes, and snippets.

@joost
Created January 30, 2013 19:50
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joost/4676207 to your computer and use it in GitHub Desktop.
Save joost/4676207 to your computer and use it in GitHub Desktop.
Get time zone id of a location by latitude and longitude (lat, lng).
# See: https://developers.google.com/maps/documentation/timezone/
# Uses HTTParty gem (https://github.com/jnunemaker/httparty).
# Usage:
# GoogleTimezone.search(:lat => 51.38494009999999, :lng => -0.3514683)['timeZoneId']
class GoogleTimezone
include HTTParty
base_uri 'https://maps.googleapis.com'
# https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810,-119.6822510&timestamp=1331161200&sensor=true_or_false
def self.search(query)
query[:location] = "#{query.delete(:lat)},#{query.delete(:lng)}" if query[:lat] && query[:lng]
query[:sensor] ||= false
query[:timestamp] ||= Time.now.to_i
self.get('/maps/api/timezone/json', :query => query)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment