方法
實例公開方法
in_time_zone(zone = ::Time.zone) 連結
如果給定時區或已設定 Time.zone_default
,則傳回 Time.zone
中的同時時間。否則,傳回目前時間。
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