Changing a time's zone
I am using the gem metar-parser to parse a given weather report (a metar). The time is given in UTC, but once my object is returned from the gem it's time zone is +2 (for Denmark). I would like to change this to UTC but maintaining the hours.
So I found this method
module ActiveSupport
class TimeWithZone
def zone=(new_zone = ::Time.zone)
# Reinitialize with the new zone and the local time
开发者_开发问答 initialize(nil, ::Time.__send__(:get_zone, new_zone), time)
end
end
end
It might not be the most elegant way of doing this. But the problem is, should I place this time_with_zone.rb file in /lib/ ? Anyway, I get a NoMethodError from zone=
.
Did you find the code here? You could either do as John explains in his answer and create a file in the config/initializers directory or in the lib directory. However, placing the in the lib directory requires you to add the directory to the auto load path. That is done in the config/application.rb file. You should add something like:
config.autoload_paths += %W(#{config.root}/lib/)
精彩评论