travel_to测试助手的以下行为是bug还是特性?无论哪种方式,为什么以这种方式发生,并且应该DateTime。现在在Rails代码中完全避免了吗?
使用<<p> strong> ruby (2.3.1) , rails (4.2.6) 和小型试验(5.9.0): test 'traveling to 1900 (Time)' do
travel_to Time.new(1916, 1, 1, 7, 0, 0) do
puts Time.current
puts Time.now
puts DateTime.current
puts DateTime.now
puts Time.zone.now
puts Time.zone.today
end
end
=>
1916-01-01 07:00:00 UTC
1916-01-01 07:00:00 UTC
1916-01-01T07:00:00+00:00
2016-08-18T14:29:20+02:00
1916-01-01 07:00:00 UTC
1916-01-01
相似的,
test 'traveling to 1900 (DateTime)' do
travel_to DateTime.new(1916, 1, 1, 7, 0, 0) do
puts Time.current
puts Time.now
puts DateTime.current
puts DateTime.now
puts Time.zone.now
puts Time.zone.today
end
end
=>
1916-01-01 07:00:00 UTC
1916-01-01 07:00:00 UTC
1916-01-01T07:00:00+00:00
2016-08-18T14:29:24+02:00
1916-01-01 07:00:00 UTC
1916-01-01
您可以尝试使用Time.zone.now
和Time.zone.today
吗?
IMHO这是应该在Rails项目中使用的API。参见http://danilenko.org/2012/7/6/rails_timezones/