我想找到时区对象的总UTC偏移量。以下是使用TZInfo::Timezone
和ActiveSupport::TimeZone
的两个示例。最终,我想使用ActiveSupport::TimeZone
实现,但无法让它给我正确的答案。
#TZInfo implementation
tz = TZInfo::Timezone.get('America/New_York')
tz.current_period.utc_total_offset / 60 / 60
=> -4 (CORRECT)
# Rails implementation
tz = ActiveSupport::TimeZone.new("Eastern Time (US & Canada)")
tz.utc_offset / 60 / 60
=> -5 (WRONG)
为什么ActiveSupport::TimeZone
似乎无法考虑dst?我该如何解决这个问题?
答案 0 :(得分:0)
我在ActiveSupport documentation找到了这个。基本上,它正在运行tzinfo.current_period.utc_offset
而不是tzinfo.current_period.utc_total_offset
def utc_offset
if @utc_offset
@utc_offset
else
tzinfo.current_period.utc_offset if tzinfo && tzinfo.current_period
end
end
所以要完全回答我的问题:我需要以下代码......
tz = ActiveSupport::TimeZone.new("Eastern Time (US & Canada)")
tz.tzinfo.current_period.utc_total_offset / 60 / 60