基于对这个问题的回答:
在 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
在“时区转换”下查看