Jér*_*Boé 1 timezone ruby-on-rails
我有一个用 RoR 编写的后端,我需要管理来自世界各地不同同行的许多请求。我有以下问题:
config.timezone为每个请求更改是否合适?
如果是:
config.timezone?)Time.zone = 'UTC+X'如果不:
基于此问题的答案: 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
Run Code Online (Sandbox Code Playgroud)
示例代码来自http://ofps.oreilly.com/titles/9780596521424/active-support.html
查看“时区转换”下的内容
| 归档时间: |
|
| 查看次数: |
1302 次 |
| 最近记录: |