2016-07-01 66 views
0

我想從時區對象中找到總的UTC偏移量。以下是使用TZInfo::TimezoneActiveSupport::TimeZone的兩個示例。最終,我想使用ActiveSupport::TimeZone實現,但不能讓它給我正確的答案。Ruby TZInfo和Rails ActiveSupport :: Timezone UTC_offset的差異

#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

我在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 
相關問題