title: How do I calculate the offset, in hours, of a given timezone from UTC in ruby?
body: <p>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:</p>
  
  <pre><code>offset_in_hours = (TZInfo::Timezone.get(self.timezone).current_period.offset.utc_offset).to_f / 3600.0
  </code></pre>
  
  <p>But, it turns out that was returning to me the Standard Offset, not the DST offset. So for example, assume </p>
  
  <pre><code>self.timezone = "America/New_York"
  </code></pre>
  
  <p>If I run the above line, offset_in_hours = -5, not -4 as it should, given that the date today is April 1, 2012.</p>
  
  <p>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?</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>Here is some output from IRB. Note that New York is 4 hours behind UTC, not 5, because of daylight savings:</p>
  
  <pre><code>&gt;&gt; require 'tzinfo'
  =&gt; false
  &gt;&gt; timezone = "America/New_York"
  =&gt; "America/New_York"
  &gt;&gt; offset_in_hours = TZInfo::Timezone.get(timezone).current_period.utc_offset / (60*60)
  =&gt; -5
  &gt;&gt; 
  </code></pre>
  
  <p>This suggests that there is a bug in TZInfo or it is not dst-aware</p>
  
  <hr>
  
  <p><strong>Update 2</strong></p>
  
  <p>Per joelparkerhender's comments, the bug in the above code is that I was using utc_offset, not utc_total_offset.</p>
  
  <p>Thus, per my original question, the correct line of code is:</p>
  
  <pre><code>offset_in_hours = (TZInfo::Timezone.get(self.timezone).current_period.offset.utc_total_offset).to_f / 3600.0
  </code></pre>