LoginSignup
2
2

More than 5 years have passed since last update.

Heroku上でのTimeのparseでハマった

Posted at

ローカルのタイムゾーンを含む"Tue May 07 2013 04:00:00 GMT+0900 (JST)"というような文字列をパースしてUTCのActiveSupport::TimeWithZoneに変換したい場合、以下のようにする。

> Time.zone
=> (GMT +00:00) UTC
> Time.zone.parse("Tue May 07 2013 04:00:00 GMT+0900 (JST)")
=> Mon, 06 May 2013 19:00:00 UTC +00:00

しかし、これをheroku上のrails consoleで実行した場合、なぜか以下のようになってしまい、タイムゾーンが考慮されない。

> Time.zone
=> (GMT +00:00) UTC
> Time.zone.parse("Tue May 07 2013 04:00:00 GMT+0900 (JST)")
=> Tue, 07 May 2013 04:00:00 UTC +00:00

これは、Time.zone.parseの中で呼んでいるTime.parseが以下のようにパースしてしまうことが原因のようだ。

> Time.parse("Tue May 07 2013 04:00:00 GMT+0900 (JST)")
=> 2013-05-07 04:00:00 +00:00

手元の環境だと正しく動くのになんでだろう...

2
2
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
2
2