为什么使用Time.current超过Time.zone.now Rails中,当Time.zone总是设置?
问题描述:
的文档Time.current
说:为什么使用Time.current超过Time.zone.now Rails中,当Time.zone总是设置?
返回
Time.zone.now
时Time.zone
或config.time_zone
设置, 否则直接返回Time.now
。# File activesupport/lib/active_support/core_ext/time/calculations.rb, line 36 def current ::Time.zone ? ::Time.zone.now : ::Time.now end
但当Time.zone
不断不集Rails的?
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Berlin'
我注释掉config.time_zone
和我仍然得到Time.zone等于“UTC”,因为它显然是设置在默认情况下的注解中提到的。那么,使用Time.current
而不是Time.zone.now
有什么意义?
PS:我on Rails的4.1.16
答
除了有清洁剂/短的语法,如果你正在使用Rails
因为是观察这个简单的答案是没有什么区别。
Time.zone_default获取ActiveSupport::Railtie
通过active_support.initialize_time_zone
初始化。有关railtie初始化过程here的更多信息。
我的理由此次检查的猜测是,作为一个框架,它被占情况下有人从他们的轨道引导过程除去这active_support.initialize_time_zone
。在这种情况下,Time.zone
将是nil
。