Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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
YumaInaura
Ruby on Rails 業務経験 約5年 / Perl PHP Python Golang Linux Apache MySQL BigQuery Jenkins ansible AWS など / いなうらゆうま / YumaInaura / 稲浦悠馬
http://twitter.com/yumainaura
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした