I have a model that has a latitude, longitude, and datetime attributes and I want to be able to calculate the timezone of that location and set it for each individual instance of the model. Here is the code that I have written to get the time zone is there something I am missing?
require 'nokogiri'
require 'open-uri'
before_validation :set_current_time_zone
def set_current_time_zone
Time.zone = find_timezone_based_on_location
end
def find_time_zone_based_on_location
url = "http://www.earthtools.org/timezone-1.1/#{self.latitude}/#{self.longitude}"
doc = Nokogiri::XML(open(url))
offset = doc.at_xpath('//offset').text().to_i
if offset == -5
"Eastern Time (US & Canada)"
....
elsif offset == -8
"Pacific Time (US & Canada)"
end
end
Is there something that I am missing as to why this is not setting the correct time?
I was able to get the code to work by changing the set_current_time_zone to the following:
This will find the correct Time zone and then translate that time into UTC.