7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Time.nowとTime.currentのタイムゾーンの違い

Posted at

##いきなり結論
Time.nowはENV['TZ']、Time.currentはconfig.time_zoneの値に影響される。

irb(main):001:0> ENV['TZ'] = 'UTC' # TZ環境変数をUTCにセットする
=> "UTC"
irb(main):002:0> Time.now
=> 2016-12-18 09:19:18 +0000
irb(main):003:0> Time.current
=> Sun, 18 Dec 2016 18:19:21 JST +09:00

irb(main):007:0> ENV['TZ'] = 'Asia/Tokyo' # TZ環境変数をAsia/Tokyoに変更する
=> "Asia/Tokyo"
irb(main):008:0> Time.now
=> 2016-12-18 18:21:45 +0900
irb(main):009:0> Time.current
=> Sun, 18 Dec 2016 18:21:48 JST +09:00

irb(main):004:0> ENV['TZ'] = 'JST' # TZ環境変数を無効な値にしてみる
=> "JST"
irb(main):005:0> Time.now
=> 2016-12-18 09:19:44 +0000
irb(main):006:0> Time.current
=> Sun, 18 Dec 2016 18:19:46 JST +09:00
# application.rbで、config.time_zone = 'UTC'にセットする
irb(main):002:0> Time.now
=> 2016-12-18 09:34:37 +0000
irb(main):003:0> Time.current
=> Sun, 18 Dec 2016 09:34:40 UTC +00:00

# application.rbで、config.time_zone = 'Tokyo'にセットする
irb(main):001:0> Time.now
=> 2016-12-18 09:36:10 +0000
irb(main):002:0> Time.current
=> Sun, 18 Dec 2016 18:36:12 JST +09:00
7
5
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
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?