2017-08-17 36 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

相關問題