Activesupport::TimeZone
Railsが日時を扱うときは、自動的にActivesupport::TimeZoneクラスを用い、タイムゾーンはデフォルトでUTC(世界協定時刻)
である
デフォルトのタイムゾーンを日本時間に変更
日本で利用するアプリケーションならば、
config.time_zone = ‘Asia/Tokyo’
を設定しておくと便利なため
タイムゾーンをデフォルトのUTCから日本時間に変更してみる
config/application.rb
module SampleApp
class Application < Rails::Application
#Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2
config.time_zone = 'Asia/Tokyo'
end
end
コンソールを立ち上げてTime.zone.now
または Time.current
を実行して
JST(日本標準時間)が表示されているか確認する
[1] pry(main)> Time.zone.now
=> Wed, 13 Mar 2019 01:19:55 JST +09:00
[2] pry(main)> Time.current
=> Wed, 13 Mar 2019 01:19:55 JST +09:00
最後に、画面上の時刻もJSTで表示されているのか確認する