I’m using ActiveSupport’s Timezone class to find the offset of a Timezone like so.
novdate = Date.new(2012,11,04)
offset_in_seconds = ActiveSupport::TimeZone['Eastern Time (US & Canada)'].at(novdate.to_time.midnight+3.hours).utc_offset
The problem is that offset_in_seconds is different on my local machine and on a heroku server. I’m not sure why this is happening. I’ve set config.time_zone = ‘Eastern Time (US & Canada)’ in my rails application.rb file. How can I accurately and consistently get a timezone offset for a given date? I’ve been adding 3 hours to the time above because the time should have been changed from DST to non-DST at 3:00am on a given date.
— display from my local rails console —
novdate = Date.new(2012,11,04)
ActiveSupport::TimeZone['Eastern Time (US & Canada)'].at(novdate.to_time.midnight+1.hours)
=> Sun, 04 Nov 2012 01:00:00 EDT -04:00
ActiveSupport::TimeZone['Eastern Time (US & Canada)'].at(novdate.to_time.midnight+2.hours)
=> Sun, 04 Nov 2012 01:00:00 EST -05:00
ActiveSupport::TimeZone['Eastern Time (US & Canada)'].at(novdate.to_time.midnight+3.hours)
=> Sun, 04 Nov 2012 02:00:00 EST -05:00
— output from a heroku rails console —
ActiveSupport::TimeZone['Eastern Time (US & Canada)'].at(novdate.to_time.midnight+4.hours)
=> Sun, 04 Nov 2012 00:00:00 EDT -04:00
ActiveSupport::TimeZone['Eastern Time (US & Canada)'].at(novdate.to_time.midnight+5.hours)
=> Sun, 04 Nov 2012 01:00:00 EDT -04:00
ActiveSupport::TimeZone['Eastern Time (US & Canada)'].at(novdate.to_time.midnight+6.hours)
=> Sun, 04 Nov 2012 01:00:00 EST -05:00
The problem here is the
to_timemethod ofdatewill not respect your timezone with that syntax.If you look at the documentation for
to_timeyou can see it converts it to local time to the server.Chances are the heroku server has a different time zone configured to your local machine (note this is not the
config.time_zonesetting)This should work: