2

如何在America/Toronto考虑 DST 更改的同时以 tzinfo 格式获取时区,即以秒为单位的 utc 偏移量。

例如,DST 设置上次更改时间为 2014 年 3 月 9 日凌晨 2:00。见偏移差异

Varinder $ irb

2.1.0 :001 > Time.local(2014,03,9,1,59) 
 => 2014-03-09 01:59:00 -0500 

2.1.0 :002 > Time.local(2014,03,9,2,00) 
 => 2014-03-09 03:00:00 -0400 
2.1.0 :003 >

2.1.0 :006 > Time.local(2014,03,9,2,00).zone
 => "EDT"  # I need America/Toronto format

对于这两个时间示例,计算的 tzinfo 字符串应该相同,因为只有偏移量的差异是由于 DST 造成的。

4

2 回答 2

1

尝试这个

zone_abbr = Time.local(2014,03,9,2,00).zone
ActiveSupport::TimeZone.
  all.
  select{|tz| tz.tzinfo.current_period.abbreviation.to_s == zone_abbr}.
  map(&:tzinfo).
  map(&:name)

对于"EDT"此代码返回["America/New_York", "America/Indiana/Indianapolis"]

于 2014-09-11T08:24:47.200 回答
1

You'll have to figure out the DST issues, but this might get you there (I'm in PST...)

> offset = Time.local(2014,03,9,1,59).utc_offset
=> -28800
> ActiveSupport::TimeZone.all.select{|tz| tz.utc_offset == offset}
=> [#<ActiveSupport::TimeZone:0x007fbcacd075a8 @name="Pacific Time (US & Canada)", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: America/Los_Angeles>, @current_period=#<TZInfo::TimezonePeriod: #<TZInfo::TimezoneTransitionInfo: #<TZInfo::TimeOrDateTime: 1394359200>,#<TZInfo::TimezoneOffsetInfo: -28800,3600,PDT>>,#<TZInfo::TimezoneTransitionInfo: #<TZInfo::TimeOrDateTime: 1414918800>,#<TZInfo::TimezoneOffsetInfo: -28800,0,PST>>>>, #<ActiveSupport::TimeZone:0x007fbcacd07530 @name="Tijuana", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: America/Tijuana>, @current_period=#<TZInfo::TimezonePeriod: #<TZInfo::TimezoneTransitionInfo: #<TZInfo::TimeOrDateTime: 1394359200>,#<TZInfo::TimezoneOffsetInfo: -28800,3600,PDT>>,#<TZInfo::TimezoneTransitionInfo: #<TZInfo::TimeOrDateTime: 1414918800>,#<TZInfo::TimezoneOffsetInfo: -28800,0,PST>>>>]
于 2014-03-15T21:03:39.307 回答