方法
實例公開方法
in_time_zone(zone = ::Time.zone) 連結
若提供區域,則傳回 `Time.zone` 中的同時時間,或如果將 Time.zone_default
設定為 on 。否則,傳回目前時間。
Time.zone = 'Hawaii' # => 'Hawaii'
Time.utc(2000).in_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00
Date.new(2000).in_time_zone # => Sat, 01 Jan 2000 00:00:00 HST -10:00
此方法類似於 Time#localtime,不同點在於它使用 `Time.zone` 作為當地區域,而不是作業系統的時區。
你也可以傳入一個 TimeZone 實例或字串作為引數,以識別 TimeZone,並且轉換會基於該區域,而不是 `Time.zone`。
Time.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
Date.new(2000).in_time_zone('Alaska') # => Sat, 01 Jan 2000 00:00:00 AKST -09:00
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_and_time/zones.rb, line 20 def in_time_zone(zone = ::Time.zone) time_zone = ::Time.find_zone! zone time = acts_like?(:time) ? self : nil if time_zone time_with_zone(time, time_zone) else time || to_time end end