跳至内容 跳至搜索

Active Support 时区

TimeZone 类是对 TZInfo::Timezone 实例的包装器。 它允许我们执行以下操作

  • 将 TZInfo 提供的时区集限制为 134 个有意义的子集。

  • 使用更友好的名称检索和显示时区(例如,使用“东部时间(美国和加拿大)”而不是“America/New_York”)。

  • 仅在需要时延迟加载 TZInfo::Timezone 实例。

  • 创建 ActiveSupport::TimeWithZone 实例通过 TimeZone 的 localparseatnow 方法。

如果您在 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
包含的模块

常量

MAPPING = { "国际日期变更线西" => "Etc/GMT+12", "中途岛" => "Pacific/Midway", "美属萨摩亚" => "Pacific/Pago_Pago", "夏威夷" => "Pacific/Honolulu", "阿拉斯加" => "America/Juneau", "太平洋时间(美国和加拿大)" => "America/Los_Angeles", "蒂华纳" => "America/Tijuana", "山区时间(美国和加拿大)" => "America/Denver", "亚利桑那州" => "America/Phoenix", "奇瓦瓦州" => "America/Chihuahua", "马萨特兰" => "America/Mazatlan", "中部时间(美国和加拿大)" => "America/Chicago", "萨斯喀彻温省" => "America/Regina", "瓜达拉哈拉" => "America/Mexico_City", "墨西哥城" => "America/Mexico_City", "蒙特雷" => "America/Monterrey", "中美洲" => "America/Guatemala", "东部时间(美国和加拿大)" => "America/New_York", "印第安纳州(东部)" => "America/Indiana/Indianapolis", "波哥大" => "America/Bogota", "利马" => "America/Lima", "基多" => "America/Lima", "大西洋时间(加拿大)" => "America/Halifax", "加拉加斯" => "America/Caracas", "拉巴斯" => "America/La_Paz", "圣地亚哥" => "America/Santiago", "纽芬兰" => "America/St_Johns", "巴西利亚" => "America/Sao_Paulo", "布宜诺斯艾利斯" => "America/Argentina/Buenos_Aires", "蒙得维的亚" => "America/Montevideo", "乔治敦" => "America/Guyana", "波多黎各" => "America/Puerto_Rico", "格陵兰岛" => "America/Godthab", "中大西洋" => "Atlantic/South_Georgia", "亚速尔群岛" => "Atlantic/Azores", "佛得角群岛" => "Atlantic/Cape_Verde", "都柏林" => "Europe/Dublin", "爱丁堡" => "Europe/London", "里斯本" => "Europe/Lisbon", "伦敦" => "Europe/London", "卡萨布兰卡" => "Africa/Casablanca", "蒙罗维亚" => "Africa/Monrovia", "UTC" => "Etc/UTC", "贝尔格莱德" => "Europe/Belgrade", "布拉迪斯拉发" => "Europe/Bratislava", "布达佩斯" => "Europe/Budapest", "卢布尔雅那" => "Europe/Ljubljana", "布拉格" => "Europe/Prague", "萨拉热窝" => "Europe/Sarajevo", "斯科普里" => "Europe/Skopje", "华沙" => "Europe/Warsaw", "萨格勒布" => "Europe/Zagreb", "布鲁塞尔" => "Europe/Brussels", "哥本哈根" => "Europe/Copenhagen", "马德里" => "Europe/Madrid", "巴黎" => "Europe/Paris", "阿姆斯特丹" => "Europe/Amsterdam", "柏林" => "Europe/Berlin", "伯尔尼" => "Europe/Zurich", "苏黎世" => "Europe/Zurich", "罗马" => "Europe/Rome", "斯德哥尔摩" => "Europe/Stockholm", "维也纳" => "Europe/Vienna", "西非中部" => "Africa/Algiers", "布加勒斯特" => "Europe/Bucharest", "开罗" => "Africa/Cairo", "赫尔辛基" => "Europe/Helsinki", "基辅" => "Europe/Kiev", "里加" => "Europe/Riga", "索非亚" => "Europe/Sofia", "塔林" => "Europe/Tallinn", "维尔纽斯" => "Europe/Vilnius", "雅典" => "Europe/Athens", "伊斯坦布尔" => "Europe/Istanbul", "明斯克" => "Europe/Minsk", "耶路撒冷" => "Asia/Jerusalem", "哈拉雷" => "Africa/Harare", "比勒陀利亚" => "Africa/Johannesburg", "加里宁格勒" => "Europe/Kaliningrad", "莫斯科" => "Europe/Moscow", "圣彼得堡" => "Europe/Moscow", "伏尔加格勒" => "Europe/Volgograd", "萨马拉" => "Europe/Samara", "科威特" => "Asia/Kuwait", "利雅得" => "Asia/Riyadh", "内罗毕" => "Africa/Nairobi", "巴格达" => "Asia/Baghdad", "德黑兰" => "Asia/Tehran", "阿布扎比" => "Asia/Muscat", "马斯喀特" => "Asia/Muscat", "巴库" => "Asia/Baku", "第比利斯" => "Asia/Tbilisi", "埃里温" => "Asia/Yerevan", "喀布尔" => "Asia/Kabul", "叶卡捷琳堡" => "Asia/Yekaterinburg", "伊斯兰堡" => "Asia/Karachi", "卡拉奇" => "Asia/Karachi", "塔什干" => "Asia/Tashkent", "金奈" => "Asia/Kolkata", "加尔各答" => "Asia/Kolkata", "孟买" => "Asia/Kolkata", "新德里" => "Asia/Kolkata", "加德满都" => "Asia/Kathmandu", "达卡" => "Asia/Dhaka", "斯里兰卡" => "Asia/Colombo", "阿拉木图" => "Asia/Almaty", "阿斯塔纳" => "Asia/Almaty", "新西伯利亚" => "Asia/Novosibirsk", "仰光" => "Asia/Rangoon", "曼谷" => "Asia/Bangkok", "河内" => "Asia/Bangkok", "雅加达" => "Asia/Jakarta", "克拉斯诺亚尔斯克" => "Asia/Krasnoyarsk", "北京" => "Asia/Shanghai", "重庆" => "Asia/Chongqing", "香港" => "Asia/Hong_Kong", "乌鲁木齐" => "Asia/Urumqi", "吉隆坡" => "Asia/Kuala_Lumpur", "新加坡" => "Asia/Singapore", "台北" => "Asia/Taipei", "珀斯" => "Australia/Perth", "伊尔库茨克" => "Asia/Irkutsk", "乌兰巴托" => "Asia/Ulaanbaatar", "首尔" => "Asia/Seoul", "大阪" => "Asia/Tokyo", "札幌" => "Asia/Tokyo", "东京" => "Asia/Tokyo", "雅库茨克" => "Asia/Yakutsk", "达尔文" => "Australia/Darwin", "阿德莱德" => "Australia/Adelaide", "堪培拉" => "Australia/Canberra", "墨尔本" => "Australia/Melbourne", "悉尼" => "Australia/Sydney", "布里斯班" => "Australia/Brisbane", "霍巴特" => "Australia/Hobart", "符拉迪沃斯托克" => "Asia/Vladivostok", "关岛" => "Pacific/Guam", "莫尔斯比港" => "Pacific/Port_Moresby", "马加丹" => "Asia/Magadan", "斯列德涅科利姆斯克" => "Asia/Srednekolymsk", "所罗门群岛" => "Pacific/Guadalcanal", "新喀里多尼亚" => "Pacific/Noumea", "斐济" => "Pacific/Fiji", "堪察加" => "Asia/Kamchatka", "马绍尔群岛" => "Pacific/Majuro", "奥克兰" => "Pacific/Auckland", "惠灵顿" => "Pacific/Auckland", "努库阿洛法" => "Pacific/Tongatapu", "托克劳群岛" => "Pacific/Fakaofo", "查塔姆群岛" => "Pacific/Chatham", "萨摩亚" => "Pacific/Apia" }
 

键是 Rails TimeZone 名称,值是 TZInfo 标识符。

属性

[R] name
[R] tzinfo

类公共方法

[](arg)

查找特定时区对象。 如果参数是字符串,则将其解释为要查找的时区的名称。 如果它是数值,则它是要查找的时区的小时偏移量或秒偏移量。 (将返回具有该偏移量的第一个时区。)如果系统中没有此类时区,则返回 nil

# 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()

返回所有 TimeZone 对象的数组。 在许多情况下,每个时区都有多个 TimeZone 对象,以方便用户查找自己的时区。

# File activesupport/lib/active_support/values/time_zone.rb, line 223
def all
  @zones ||= zones_map.values.sort
end

country_zones(country_code)

一个方便的方法,用于返回由其 ISO 3166-1 Alpha2 代码指定的国家/地区中的时区的 TimeZone 对象的集合。

# 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)。

# File activesupport/lib/active_support/values/time_zone.rb, line 300
    

find_tzinfo(name)

# File activesupport/lib/active_support/values/time_zone.rb, line 207
def find_tzinfo(name)
  TZInfo::Timezone.get(MAPPING[name] || name)
end

new(name)

返回具有给定名称的 TimeZone 实例,如果不存在此类 TimeZone 实例,则返回 nil。 (这存在于支持使用 composed_of 宏来使用此类时。)

# 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"
# 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 对象的集合。

# File activesupport/lib/active_support/values/time_zone.rb, line 254
def us_zones
  country_zones(:us)
end

实例公共方法

<=>(zone)

将此时区与参数进行比较。首先比较它们的偏移量,然后按名称比较。

# 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 标识符与提供的正则表达式进行比较,如果找到匹配项,则返回 true

# File activesupport/lib/active_support/values/time_zone.rb, line 342
def =~(re)
  re === name || re === MAPPING[name]
end

at(*args)

用于从 Unix 纪元以来的秒数创建新的 ActiveSupport::TimeWithZone 实例,该实例位于 self 的时区中。

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
# 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"
# File activesupport/lib/active_support/values/time_zone.rb, line 327
def formatted_offset(colon = true, alternate_utc_string = nil)
  utc_offset == 0 && alternate_utc_string || self.class.seconds_to_utc_offset(utc_offset, colon)
end

iso8601(str)

用于从 ISO 8601 字符串创建新的 ActiveSupport::TimeWithZone 实例,该实例位于 self 的时区中。

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)

用于从给定值创建新的 ActiveSupport::TimeWithZone 实例,该实例位于 self 的时区中。

Time.zone = 'Hawaii'                    # => "Hawaii"
Time.zone.local(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00
# File activesupport/lib/active_support/values/time_zone.rb, line 363
def local(*args)
  time = Time.utc(*args)
  ActiveSupport::TimeWithZone.new(nil, self, time)
end

local_to_utc(time, dst = true)

将给定时间调整到 UTC 中的同步时间。返回一个 Time.utc() 实例。

# File activesupport/lib/active_support/values/time_zone.rb, line 551
def local_to_utc(time, dst = true)
  tzinfo.local_to_utc(time, dst)
end

match?(re)

name 和 TZInfo 标识符与提供的正则表达式进行比较,如果找到匹配项,则返回 true

# File activesupport/lib/active_support/values/time_zone.rb, line 348
def match?(re)
  (re == name) || (re == MAPPING[name]) ||
    ((Regexp === re) && (re.match?(name) || re.match?(MAPPING[name])))
end

now()

返回一个 ActiveSupport::TimeWithZone 实例,该实例表示 self 所代表时区中的当前时间。

Time.zone = 'Hawaii'  # => "Hawaii"
Time.zone.now         # => Wed, 23 Jan 2008 20:24:27 HST -10:00
# File activesupport/lib/active_support/values/time_zone.rb, line 516
def now
  time_now.utc.in_time_zone(self)
end

parse(str, now = now())

用于从解析的字符串创建新的 ActiveSupport::TimeWithZone 实例,该实例位于 self 的时区中。

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

# File activesupport/lib/active_support/values/time_zone.rb, line 453
def parse(str, now = now())
  parts_to_time(Date._parse(str, false), now)
end

rfc3339(str)

用于从 RFC 3339 字符串创建新的 ActiveSupport::TimeWithZone 实例,该实例位于 self 的时区中。

Time.zone = 'Hawaii'                     # => "Hawaii"
Time.zone.rfc3339('2000-01-01T00:00:00Z') # => Fri, 31 Dec 1999 14:00:00 HST -10:00

如果时间或区域组件丢失,则将引发 ArgumentError。这比 parseiso8601 严格得多,后者允许缺少组件。

Time.zone = 'Hawaii'            # => "Hawaii"
Time.zone.rfc3339('1999-12-31') # => ArgumentError: invalid date
# 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.

假设 strself 时区中的时间,除非 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
# 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()

返回此时区的文本表示形式。

# File activesupport/lib/active_support/values/time_zone.rb, line 354
def to_s
  "(GMT#{formatted_offset}) #{name}"
end

today()

返回此时区中的当前日期。

# File activesupport/lib/active_support/values/time_zone.rb, line 521
def today
  tzinfo.now.to_date
end

tomorrow()

返回此时区中的下一个日期。

# File activesupport/lib/active_support/values/time_zone.rb, line 526
def tomorrow
  today + 1
end

utc_offset()

以秒为单位返回此时区相对于 UTC 的偏移量。

# 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 返回一个具有非零 utc_offset 的 Time。有关更多信息,请参阅 utc_to_local_returns_utc_offset_times 配置。

# 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()

返回此时区中的上一个日期。

# File activesupport/lib/active_support/values/time_zone.rb, line 531
def yesterday
  today - 1
end