Skip to content

Commit

Permalink
String#to_time changed defaults from :utc to :local
Browse files Browse the repository at this point in the history
This commit monkey patches to_time to default back to :utc, but emits a
warning if you don't pass a value in to to_time().  Any calls to
`to_time` in the current code base should pass :utc.  This monkey patch
will make to_time backwards compatible until we find all the spots where
we don't pass the right conversion type in.

The default was changed [here](rails/rails@b79adc4).
  • Loading branch information
tenderlove committed Mar 13, 2015
1 parent a7cb033 commit be8403d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vmdb/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1750,7 +1750,7 @@ def set_edit_timer_from_schedule(schedule)
@edit[:new][:timer_weeks] = schedule.run_at[:interval][:value] if schedule.run_at[:interval][:unit] == "weekly"
@edit[:new][:timer_days] = schedule.run_at[:interval][:value] if schedule.run_at[:interval][:unit] == "daily"
@edit[:new][:timer_hours] = schedule.run_at[:interval][:value] if schedule.run_at[:interval][:unit] == "hourly"
t = schedule.run_at[:start_time].to_time.in_time_zone(@edit[:tz])
t = schedule.run_at[:start_time].to_time(:utc).in_time_zone(@edit[:tz])
@edit[:new][:start_hour] = t.strftime("%H")
@edit[:new][:start_min] = t.strftime("%M")
end
Expand Down
17 changes: 17 additions & 0 deletions vmdb/config/initializers/as_to_time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'active_support/core_ext/string/conversions'
require 'active_support/deprecation'

class String
alias_method :old_to_time, :to_time

OBJ = Object.new

def to_time(form = OBJ)
if form == OBJ
ActiveSupport::Deprecation.warn "Rails 4 changes the default of String#to_time to local. Please pass the type of conversion you want, like to_time(:utc) or to_time(:local)", caller.drop(1)
old_to_time(:utc)
else
old_to_time(form)
end
end
end

0 comments on commit be8403d

Please sign in to comment.