Mag*_*gne 6 time timezone ruby-on-rails
文档Time.current说:
返回
Time.zone.now何时Time.zone或config.time_zone设置,否则只返回Time.now.Run Code Online (Sandbox Code Playgroud)# 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'
Run Code Online (Sandbox Code Playgroud)
我已经注释掉了config.time_zone,我仍然将Time.zone等于'UTC',因为它显然会默认设置为注释提及.那么,使用Time.current结束有Time.zone.now什么意义呢?
PS:我在Rails 4.1.16上观察到这一点
简短的回答是,如果您Rails按原样使用清除/缩短语法,则没有区别.
Time.zone_default在ActiveSupport::Railtievia中初始化active_support.initialize_time_zone.在railtie初始化过程更多信息在这里.
我猜测这个检查的原因是,作为一个框架,它考虑了有人active_support.initialize_time_zone从他们的rails启动过程中删除它的情况.在这种情况下Time.zone会nil.
| 归档时间: |
|
| 查看次数: |
3378 次 |
| 最近记录: |