# offset = TimeZoneFinder.gmt_offset(some_lat, some_lng)
# timezone = ActiveSupport::TimeZone[offset]
#
# timezone = TimeZoneFinder.timezoneId(lat, lng)
# Time.zone = timezone
# Time.zone.now
#
class TimeZoneFinder
attr_accessor :result
def self.gmt_offset(lat,lng)
self.new(lat,lng).find('gmtOffset').to_f
end
def self.timezoneId(lat,lng)
self.new(lat,lng).find('timezoneId')
end
def initialize(lat,lng)
uri = URI.parse("http://ws.geonames.org/timezone?lat=#{lat}&lng=#{lng}")
@result = Hash.from_xml(open(uri))
end
def find(key)
result["geonames"]["timezone"][key]
end
end
# Example XML response...
#
#
# US
# United States
# 41.8781136
# -87.6297982
# America/Chicago
# -5.0
# -6.0
# -6.0
# 2011-09-01 21:40
# 2011-09-02 06:16
# 2011-09-02 19:22
#
# `enter code here`
2012-10-10 05:06:33
akaco