: Convert Time from one time zone to another in Rails
好像使用config.timezone是由于线程问题是一个坏主意。
我会推荐使用ActiveSupport TimeZone和TimeWithZone类。 这可以让你的线沿线做的事情:
# Return the simultaneous time in Time.zone or the specified zone
Time.now.in_time_zone # => Tue, 13 Jul 2010 01:20:55 EDT -04:00
Time.now.in_time_zone(Time.zone) # => Tue, 13 Jul 2010 01:20:55 EDT -04:00
Time.now.in_time_zone("Asia/Vladivostok") # => Tue, 13 Jul 2010 16:20:55 VLAST +11:00
Time.now.in_time_zone(-3.hours) # => Tue, 13 Jul 2010 02:20:55 BRT -03:00
# Switch to a given timezone within a block
Time.use_zone "Asia/Vladivostok" do
Time.zone.now # => Tue, 13 Jul 2010 16:20:55 VLAST +11:00
end
示例代码是从http://ofps.oreilly.com/titles/9780596521424/active-support.html
采取下“时区转换”
我没有找到如何改变Time.zone与UTC偏移,如'Time.zone =“UTC + X”' –
最后找到它:'Time.zone = X'。只是。 –