How do I calculate the offset, in hours, of a given timezone from UTC in ruby?

Posted by esilver on Stack Overflow See other posts from Stack Overflow or by esilver
Published on 2012-04-01T05:04:24Z Indexed on 2012/04/01 5:28 UTC
Read the original article Hit count: 197

Filed under:
|

I need to calculate the offset, in hours, of a given timezone from UTC in Ruby. This line of code had been working for me, or so I thought:

offset_in_hours = (TZInfo::Timezone.get(self.timezone).current_period.offset.utc_offset).to_f / 3600.0

But, it turns out that was returning to me the Standard Offset, not the DST offset. So for example, assume

self.timezone = "America/New_York"

If I run the above line, offset_in_hours = -5, not -4 as it should, given that the date today is April 1, 2012.

Can anyone advise me how to calculate offset_in_hours from UTC given a valid string TimeZone in Ruby that accounts for both standard time and daylight savings?

Thanks!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about timezone