Active 支援時區
TimeZone
類別用途是包裝 TZInfo::Timezone
實例中。允許我們執行以下動作:
-
將 TZInfo 提供的區域組限制為 134 個區域的有意義的子集。
-
以友善名稱 (例如「美國和加拿大東部時間」,取代「美洲/紐約」) 擷取並顯示區域。
-
僅在需要時才延遲載入
TZInfo::Timezone
實例。 -
透過 TimeZone 的
local
、parse
、at
及now
方法,建立ActiveSupport::TimeWithZone
實例。
如果您在 Rails 應用程式中設定 config.time_zone
,便可透過 Time.zone
來存取這個 TimeZone
物件。
# application.rb:
class Application < Rails::Application
config.time_zone = 'Eastern Time (US & Canada)'
end
Time.zone # => #<ActiveSupport::TimeZone:0x514834...>
Time.zone.name # => "Eastern Time (US & Canada)"
Time.zone.now # => Sun, 18 May 2008 14:30:44 EDT -04:00
- #
- A
- C
- F
- I
- L
- M
- N
- P
- R
- S
- T
- U
- Y
- Comparable
常數
MAPPING | = | { "International Date Line West" => "Etc/GMT+12", "Midway Island" => "Pacific/Midway", "American Samoa" => "Pacific/Pago_Pago", "Hawaii" => "Pacific/Honolulu", "Alaska" => "America/Juneau", "Pacific Time (US & Canada)" => "America/Los_Angeles", "Tijuana" => "America/Tijuana", "Mountain Time (US & Canada)" => "America/Denver", "Arizona" => "America/Phoenix", "Chihuahua" => "America/Chihuahua", "Mazatlan" => "America/Mazatlan", "Central Time (US & Canada)" => "America/Chicago", "Saskatchewan" => "America/Regina", "Guadalajara" => "America/Mexico_City", "Mexico City" => "America/Mexico_City", "Monterrey" => "America/Monterrey", "Central America" => "America/Guatemala", "Eastern Time (US & Canada)" => "America/New_York", "Indiana (East)" => "America/Indiana/Indianapolis", "Bogota" => "America/Bogota", "Lima" => "America/Lima", "Quito" => "America/Lima", "Atlantic Time (Canada)" => "America/Halifax", "Caracas" => "America/Caracas", "La Paz" => "America/La_Paz", "Santiago" => "America/Santiago", "Newfoundland" => "America/St_Johns", "Brasilia" => "America/Sao_Paulo", "Buenos Aires" => "America/Argentina/Buenos_Aires", "Montevideo" => "America/Montevideo", "Georgetown" => "America/Guyana", "Puerto Rico" => "America/Puerto_Rico", "Greenland" => "America/Godthab", "Mid-Atlantic" => "Atlantic/South_Georgia", "Azores" => "Atlantic/Azores", "Cape Verde Is." => "Atlantic/Cape_Verde", "Dublin" => "Europe/Dublin", "Edinburgh" => "Europe/London", "Lisbon" => "Europe/Lisbon", "London" => "Europe/London", "Casablanca" => "Africa/Casablanca", "Monrovia" => "Africa/Monrovia", "UTC" => "Etc/UTC", "Belgrade" => "Europe/Belgrade", "Bratislava" => "Europe/Bratislava", "Budapest" => "Europe/Budapest", "Ljubljana" => "Europe/Ljubljana", "Prague" => "Europe/Prague", "Sarajevo" => "Europe/Sarajevo", "Skopje" => "Europe/Skopje", "Warsaw" => "Europe/Warsaw", "Zagreb" => "Europe/Zagreb", "Brussels" => "Europe/Brussels", "Copenhagen" => "Europe/Copenhagen", "Madrid" => "Europe/Madrid", "Paris" => "Europe/Paris", "Amsterdam" => "Europe/Amsterdam", "Berlin" => "Europe/Berlin", "Bern" => "Europe/Zurich", "Zurich" => "Europe/Zurich", "Rome" => "Europe/Rome", "Stockholm" => "Europe/Stockholm", "Vienna" => "Europe/Vienna", "West Central Africa" => "Africa/Algiers", "Bucharest" => "Europe/Bucharest", "Cairo" => "Africa/Cairo", "Helsinki" => "Europe/Helsinki", "Kyiv" => "Europe/Kiev", "Riga" => "Europe/Riga", "Sofia" => "Europe/Sofia", "Tallinn" => "Europe/Tallinn", "Vilnius" => "Europe/Vilnius", "Athens" => "Europe/Athens", "Istanbul" => "Europe/Istanbul", "Minsk" => "Europe/Minsk", "Jerusalem" => "Asia/Jerusalem", "Harare" => "Africa/Harare", "Pretoria" => "Africa/Johannesburg", "Kaliningrad" => "Europe/Kaliningrad", "Moscow" => "Europe/Moscow", "St. Petersburg" => "Europe/Moscow", "Volgograd" => "Europe/Volgograd", "Samara" => "Europe/Samara", "Kuwait" => "Asia/Kuwait", "Riyadh" => "Asia/Riyadh", "Nairobi" => "Africa/Nairobi", "Baghdad" => "Asia/Baghdad", "Tehran" => "Asia/Tehran", "Abu Dhabi" => "Asia/Muscat", "Muscat" => "Asia/Muscat", "Baku" => "Asia/Baku", "Tbilisi" => "Asia/Tbilisi", "Yerevan" => "Asia/Yerevan", "Kabul" => "Asia/Kabul", "Ekaterinburg" => "Asia/Yekaterinburg", "Islamabad" => "Asia/Karachi", "Karachi" => "Asia/Karachi", "Tashkent" => "Asia/Tashkent", "Chennai" => "Asia/Kolkata", "Kolkata" => "Asia/Kolkata", "Mumbai" => "Asia/Kolkata", "New Delhi" => "Asia/Kolkata", "Kathmandu" => "Asia/Kathmandu", "Dhaka" => "Asia/Dhaka", "Sri Jayawardenepura" => "Asia/Colombo", "Almaty" => "Asia/Almaty", "Astana" => "Asia/Almaty", "Novosibirsk" => "Asia/Novosibirsk", "Rangoon" => "Asia/Rangoon", "Bangkok" => "Asia/Bangkok", "Hanoi" => "Asia/Bangkok", "Jakarta" => "Asia/Jakarta", "Krasnoyarsk" => "Asia/Krasnoyarsk", "Beijing" => "Asia/Shanghai", "Chongqing" => "Asia/Chongqing", "Hong Kong" => "Asia/Hong_Kong", "Urumqi" => "Asia/Urumqi", "Kuala Lumpur" => "Asia/Kuala_Lumpur", "Singapore" => "Asia/Singapore", "Taipei" => "Asia/Taipei", "Perth" => "Australia/Perth", "Irkutsk" => "Asia/Irkutsk", "Ulaanbaatar" => "Asia/Ulaanbaatar", "Seoul" => "Asia/Seoul", "Osaka" => "Asia/Tokyo", "Sapporo" => "Asia/Tokyo", "Tokyo" => "Asia/Tokyo", "Yakutsk" => "Asia/Yakutsk", "Darwin" => "Australia/Darwin", "Adelaide" => "Australia/Adelaide", "Canberra" => "Australia/Canberra", "Melbourne" => "Australia/Melbourne", "Sydney" => "Australia/Sydney", "Brisbane" => "Australia/Brisbane", "Hobart" => "Australia/Hobart", "Vladivostok" => "Asia/Vladivostok", "Guam" => "Pacific/Guam", "Port Moresby" => "Pacific/Port_Moresby", "Magadan" => "Asia/Magadan", "Srednekolymsk" => "Asia/Srednekolymsk", "Solomon Is." => "Pacific/Guadalcanal", "New Caledonia" => "Pacific/Noumea", "Fiji" => "Pacific/Fiji", "Kamchatka" => "Asia/Kamchatka", "Marshall Is." => "Pacific/Majuro", "Auckland" => "Pacific/Auckland", "Wellington" => "Pacific/Auckland", "Nuku'alofa" => "Pacific/Tongatapu", "Tokelau Is." => "Pacific/Fakaofo", "Chatham Is." => "Pacific/Chatham", "Samoa" => "Pacific/Apia" } |
金鑰是 Rails |
屬性
[R] | name | |
[R] | tzinfo |
類別公開方法
[](arg) 連結
找出特定時區物件。如果引數為字串,則解釋為要找出的時區名稱。如果為數值,則為要找出的時區的時差 (小時或秒)。(會傳回此時差的第一個時區。) 如果系統不知道任何此類時區,會傳回 nil
。
來源: 顯示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 232 def [](arg) case arg when self arg when String begin @lazy_zones_map[arg] ||= create(arg) rescue TZInfo::InvalidTimezoneIdentifier nil end when TZInfo::Timezone @lazy_zones_map[arg.name] ||= create(arg.name, nil, arg) when Numeric, ActiveSupport::Duration arg *= 3600 if arg.abs <= 13 all.find { |z| z.utc_offset == arg.to_i } else raise ArgumentError, "invalid argument to TimeZone[]: #{arg.inspect}" end end
all() 連結
來源: 顯示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 223 def all @zones ||= zones_map.values.sort end
country_zones(country_code) 連結
用來傳回一個 TimeZone
物件集合的便捷方法,這些物件來自於特定國家,ISO 3166-1 Alpha2 代碼來指定國家。
來源: 顯示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 260 def country_zones(country_code) code = country_code.to_s.upcase @country_zones[code] ||= load_country_zones(code) end
create(name, utc_offset = nil, tzinfo = nil) 連結
使用指定的名称和偏移创建新的 TimeZone
对象。偏移是此时区与 UTC(GMT)的秒数偏移。选择秒作为偏移单位是因为它是 Ruby 用来表示时区偏移的单位(请参阅 Time#utc_offset)。
来源: 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 300
find_tzinfo(name) 链接
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 207 def find_tzinfo(name) TZInfo::Timezone.get(MAPPING[name] || name) end
new(name) 链接
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 216 def new(name) self[name] end
seconds_to_utc_offset(seconds, colon = true) 链接
假定 self 表示 UTC 中的偏移(秒)(从 Time#utc_offset 返回),并将此转换为 +HH:MM 格式的字符串。
ActiveSupport::TimeZone.seconds_to_utc_offset(-21_600) # => "-06:00"
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 199 def seconds_to_utc_offset(seconds, colon = true) format = colon ? UTC_OFFSET_WITH_COLON : UTC_OFFSET_WITHOUT_COLON sign = (seconds < 0 ? "-" : "+") hours = seconds.abs / 3600 minutes = (seconds.abs % 3600) / 60 format % [sign, hours, minutes] end
us_zones() 链接
用于返回美国时区的 TimeZone
对象集合的便捷方法。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 254 def us_zones country_zones(:us) end
实例公有方法
<=>(zone) 链接
将此时区与参数进行比较。首先比较偏移,然后比较名称。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 333 def <=>(zone) return unless zone.respond_to? :utc_offset result = (utc_offset <=> zone.utc_offset) result = (name <=> zone.name) if result == 0 result end
=~(re) 链接
将 name
和 TZInfo 标识符与提供的 regexp 进行比较,如果找到匹配项,则返回 true
。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 342 def =~(re) re === name || re === MAPPING[name] end
at(*args) 链接
在 self
所在时区中自 UNIX 纪元以来的秒数中创建新 ActiveSupport::TimeWithZone
实例的方法。
Time.zone = 'Hawaii' # => "Hawaii"
Time.utc(2000).to_f # => 946684800.0
Time.zone.at(946684800.0) # => Fri, 31 Dec 1999 14:00:00 HST -10:00
还可以提供一个第二个参数来指定亚秒精度。
Time.zone = 'Hawaii' # => "Hawaii"
Time.at(946684800, 123456.789).nsec # => 123456789
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 379 def at(*args) Time.at(*args).utc.in_time_zone(self) end
formatted_offset(colon = true, alternate_utc_string = nil) 連結
傳回格式化的 UTC 偏移字串,或如果時區已經是 UTC,傳回其他字串。
zone = ActiveSupport::TimeZone['Central Time (US & Canada)']
zone.formatted_offset # => "-06:00"
zone.formatted_offset(false) # => "-0600"
iso8601(str) 連結
從 ISO 8601 字串中,在 self
時區建立新的 ActiveSupport::TimeWithZone
實例的方法。
Time.zone = 'Hawaii' # => "Hawaii"
Time.zone.iso8601('1999-12-31T14:00:00') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
如果時間元件遺失,它們將會設為零。
Time.zone = 'Hawaii' # => "Hawaii"
Time.zone.iso8601('1999-12-31') # => Fri, 31 Dec 1999 00:00:00 HST -10:00
如果字串無效,將會引發 ArgumentError
,這與 parse
不同,parse
通常會在給定無效的日期字串時傳回 nil
。
# File activesupport/lib/active_support/values/time_zone.rb, line 396 def iso8601(str) # Historically `Date._iso8601(nil)` returns `{}`, but in the `date` gem versions `3.2.1`, `3.1.2`, `3.0.2`, # and `2.0.1`, `Date._iso8601(nil)` raises `TypeError` https://github.com/ruby/date/issues/39 # Future `date` releases are expected to revert back to the original behavior. raise ArgumentError, "invalid date" if str.nil? parts = Date._iso8601(str) year = parts.fetch(:year) if parts.key?(:yday) ordinal_date = Date.ordinal(year, parts.fetch(:yday)) month = ordinal_date.month day = ordinal_date.day else month = parts.fetch(:mon) day = parts.fetch(:mday) end time = Time.new( year, month, day, parts.fetch(:hour, 0), parts.fetch(:min, 0), parts.fetch(:sec, 0) + parts.fetch(:sec_fraction, 0), parts.fetch(:offset, 0) ) if parts[:offset] TimeWithZone.new(time.utc, self) else TimeWithZone.new(nil, self, time) end rescue Date::Error, KeyError raise ArgumentError, "invalid date" end
local(*args) 連結
從給定值中,在 self
時區建立新的 ActiveSupport::TimeWithZone
實例的方法。
Time.zone = 'Hawaii' # => "Hawaii"
Time.zone.local(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00
local_to_utc(time, dst = true) 連結
調整給定時間為 UTC 的同時時間。傳回 Time.utc() 實例。
now() 連結
傳回 ActiveSupport::TimeWithZone
實例,表現在 self
所表達成時區的目前時間。
Time.zone = 'Hawaii' # => "Hawaii"
Time.zone.now # => Wed, 23 Jan 2008 20:24:27 HST -10:00
parse(str, now = now()) 連結
從剖析的字串中,在 self
時區建立新的 ActiveSupport::TimeWithZone
實例的方法。
Time.zone = 'Hawaii' # => "Hawaii"
Time.zone.parse('1999-12-31 14:00:00') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
如果字串遺失較上位元件,它們會從 TimeZone#now
取得
Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
不過,如果日期元件未提供,但提供任何其他上位元件,月份的日期就會預設為 1
Time.zone.parse('Mar 2000') # => Wed, 01 Mar 2000 00:00:00 HST -10:00
如果字串無效,會引發 ArgumentError
。
rfc3339(str) 連結
透過 RFC 3339 字串在時間區域 `self` 中建立新的 `ActiveSupport::TimeWithZone
` 執行個體方法。
Time.zone = 'Hawaii' # => "Hawaii"
Time.zone.rfc3339('2000-01-01T00:00:00Z') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
如果時間或時區組件遺失,則會產生 `ArgumentError`。這比允許缺少組件的 `parse` 或 `iso8601` 嚴格得多。
Time.zone = 'Hawaii' # => "Hawaii"
Time.zone.rfc3339('1999-12-31') # => ArgumentError: invalid date
原始碼:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 469 def rfc3339(str) parts = Date._rfc3339(str) raise ArgumentError, "invalid date" if parts.empty? time = Time.new( parts.fetch(:year), parts.fetch(:mon), parts.fetch(:mday), parts.fetch(:hour), parts.fetch(:min), parts.fetch(:sec) + parts.fetch(:sec_fraction, 0), parts.fetch(:offset) ) TimeWithZone.new(time.utc, self) end
strptime(str, format, now = now()) 連結
依據 `format` 分析 `str`,並傳回 `ActiveSupport::TimeWithZone
`。
假設 `str` 是時區 `self` 中的時間,除非 `format` 包含明確時區。(這與 `parse` 行為相同。)在任何一種情況下,傳回的 `TimeWithZone
` 都具有與 `self` 相同的時區。
Time.zone = 'Hawaii' # => "Hawaii"
Time.zone.strptime('1999-12-31 14:00:00', '%Y-%m-%d %H:%M:%S') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
如果字串遺失較上位元件,它們會從 TimeZone#now
取得
Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
Time.zone.strptime('22:30:00', '%H:%M:%S') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
不過,如果日期元件未提供,但提供任何其他上位元件,月份的日期就會預設為 1
Time.zone.strptime('Mar 2000', '%b %Y') # => Wed, 01 Mar 2000 00:00:00 HST -10:00
原始碼:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 507 def strptime(str, format, now = now()) parts_to_time(DateTime._strptime(str, format), now) end
to_s() 連結
傳回此時區的文字表示。
原始碼:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 354 def to_s "(GMT#{formatted_offset}) #{name}" end
today() 連結
傳回此時區中的目前日期。
原始碼:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 521 def today tzinfo.now.to_date end
tomorrow() 連結
傳回此時區中的下一個日期。
原始碼:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 526 def tomorrow today + 1 end
utc_offset() 連結
傳回此時區與 UTC 以秒為單位的偏移量。
原始碼:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 317 def utc_offset @utc_offset || tzinfo&.current_period&.base_utc_offset end
utc_to_local(time) 連結
調整給定的時間到 `self` 所表示時區中的同時時間。傳回具有適當偏移量的當地時間 – 如果你想要 `ActiveSupport::TimeWithZone
` 執行個體,請使用 `Time#in_time_zone()
` 取代。
自 tzinfo 2 起,`utc_to_local
` 會傳回偏移量不為 0 的 `Time
`。有關詳細資訊,請參閱 `utc_to_local_returns_utc_offset_times` 設定。
原始碼:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/values/time_zone.rb, line 542 def utc_to_local(time) tzinfo.utc_to_local(time).yield_self do |t| ActiveSupport.utc_to_local_returns_utc_offset_times ? t : Time.utc(t.year, t.month, t.day, t.hour, t.min, t.sec, t.sec_fraction * 1_000_000) end end
yesterday() 連結
傳回此時區中的前一個日期。