2017-08-17 18 views
1

的文档Time.current说:为什么使用Time.current超过Time.zone.now Rails中,当Time.zone总是设置?

返回Time.zone.nowTime.zoneconfig.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

回答

1

除了有清洁剂/短的语法,如果你正在使用Rails因为是观察这个简单的答案是没有什么区别。

Time.zone_default获取ActiveSupport::Railtie通过active_support.initialize_time_zone初始化。有关railtie初始化过程here的更多信息。

我的理由此次检查的猜测是,作为一个框架,它被占情况下有人从他们的轨道引导过程除去这active_support.initialize_time_zone。在这种情况下,Time.zone将是nil

相关问题