I want to find the total UTC offset from a Timezone Object. Below are two examples using TZInfo::Timezone
and ActiveSupport::TimeZone
. Ultimately, I want to use the ActiveSupport::TimeZone
implementation, but can't get it to give me the right answer.
#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)
Why does ActiveSupport::TimeZone
appear to fail to factor in dst? How do I fix that?
I found this in the ActiveSupport documentation . Basically, it is running tzinfo.current_period.utc_offset
instead of 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
So to fully answer my question: I need the following code...
tz = ActiveSupport::TimeZone.new("Eastern Time (US & Canada)")
tz.tzinfo.current_period.utc_total_offset / 60 / 60
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.