LoginSignup
3
3

More than 1 year has passed since last update.

Rails | String#to_datetime が現地タイムゾーンを付けてくれない対策

Last updated at Posted at 2016-08-20

1. to_datetime だけの場合

現地のタイムゾーン情報が付加されない。

ENV['TZ'] = 'Asia/Tokyo'

'2016-09-06'.to_datetime
# => Tue, 06 Sep 2016 00:00:00 +0000

2. to_time を挟んだ場合

現地のタイムゾーン情報が付加される。

ENV['TZ'] = 'Asia/Tokyo'

'2016-09-06'.to_time.to_datetime
# => Tue, 06 Sep 2016 00:00:00 +0900

3. in_time_zone の場合

現地のタイムゾーン情報が付加されるが、フォーマットが異なる。

Time.zone = 'Tokyo'

'2016-09-06'.in_time_zone
# => Tue, 06 Sep 2016 00:00:00 JST +09:00

4. in_time_zone を挟んだ場合

現地のタイムゾーン情報が付加される。(2の結果と同じ)

Time.zone = 'Tokyo'

'2016-09-06'.in_time_zone.to_datetime
# => Tue, 06 Sep 2016 00:00:00 +0900

環境

  • ruby 2.3.0
  • Rails 2.4.6

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3