fedenusy
fedenusy

Reputation: 274

Rails not accounting for daylight savings time

ActiveSupport::TimeZone.new("Pacific Time (US & Canada)")
=> (GMT-08:00) Pacific Time (US & Canada)

...but right now, in LA it's GMT-07:00 because of DST. See http://www.timeanddate.com/worldclock/city.html?n=137

Is this a bug, an error in my settings, or am I creating the TimeZone object improperly? Can't seem to figure it out

Upvotes: 2

Views: 2813

Answers (2)

fedenusy
fedenusy

Reputation: 274

Figured it out. Turns out the TimeZone object does have a concept of 'now':

tz = ActiveSupport::TimeZone.new("Pacific Time (US & Canada)")
=> (GMT-08:00) Pacific Time (US & Canada) 
tz.parse('8:00AM')
=> Fri, 12 Oct 2012 08:00:00 PDT -07:00

So you can still create TimeWithZone objects with the proper offset using the TimeZone-- despite the fact that its to_s method displays the incorrect offset.

Upvotes: 4

rossta
rossta

Reputation: 11494

I don't believe ActiveSupport::TimeZone objects have a concept of "now". You can initialize a TimeZone with whatever utc_offset in seconds from you want with ::create:

ActiveSupport::TimeZone.create("Pacific Time (US & Canada)", -(60 * 60 * 7))
#  => (GMT-07:00) Pacific Time (US & Canada)

The concept of now in relation to daylight savings time makes more sense when dealing with ActiveSupport::TimeWithZone objects. When you make your Rails app time zone aware by setting the time zone in application.rb:

config.time_zone = 'Pacific Time (US & Canada)'

...and you use Time.zone.now, the result will take into account daylight savings time, GMT-07:00. So at the time of this post:

Time.zone.now
=> Fri, 12 Oct 2012 07:03:51 PDT -07:00

Time.zone.now.class
=> ActiveSupport::TimeWithZone

Upvotes: 1

Related Questions