diff options
author | kennyj <kennyj@gmail.com> | 2013-09-20 00:13:33 +0900 |
---|---|---|
committer | kennyj <kennyj@gmail.com> | 2013-09-25 00:08:40 +0900 |
commit | 8dd5a582f544ca86add20d46c2a62b965b28e08a (patch) | |
tree | 5a552d3a5de2835b3a90e83c55d2f05c6c44ed8b | |
parent | 16c7873c44cbced87b153bcbc7ac7ecfe950b53c (diff) | |
download | rails-8dd5a582f544ca86add20d46c2a62b965b28e08a.tar.gz rails-8dd5a582f544ca86add20d46c2a62b965b28e08a.tar.bz2 rails-8dd5a582f544ca86add20d46c2a62b965b28e08a.zip |
ActiveRecord::ConnectionAdapters::Column.string_to_time method respects string with timezone. Closes #12278.
-rw-r--r-- | activerecord/CHANGELOG.md | 7 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/column.rb | 14 | ||||
-rw-r--r-- | activerecord/test/cases/column_test.rb | 10 |
3 files changed, 28 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 12e224d27a..309cd633c3 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,10 @@ +* `ActiveRecord::ConnectionAdapters.string_to_time` respects + string with timezone (e.g. Wed, 04 Sep 2013 20:30:00 JST). + + Fixes: #12278 + + *kennyj* + * Calling `update_attributes` will now throw an `ArgumentError` whenever it gets a `nil` argument. More specifically, it will throw an error if the argument that it gets passed does not respond to to `stringify_keys`. diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index fb53090edc..2596c221bc 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -203,11 +203,19 @@ module ActiveRecord end end - def new_time(year, mon, mday, hour, min, sec, microsec) + def new_time(year, mon, mday, hour, min, sec, microsec, offset = nil) # Treat 0000-00-00 00:00:00 as nil. return nil if year.nil? || (year == 0 && mon == 0 && mday == 0) - Time.send(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil + if offset + time = Time.utc(year, mon, mday, hour, min, sec, microsec) rescue nil + return nil unless time + + time -= offset + Base.default_timezone == :utc ? time : time.getlocal + else + Time.public_send(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil + end end def fast_string_to_date(string) @@ -232,7 +240,7 @@ module ActiveRecord time_hash = Date._parse(string) time_hash[:sec_fraction] = microseconds(time_hash) - new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction)) + new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset)) end end diff --git a/activerecord/test/cases/column_test.rb b/activerecord/test/cases/column_test.rb index 3a4f414ae8..5ab2f18e9d 100644 --- a/activerecord/test/cases/column_test.rb +++ b/activerecord/test/cases/column_test.rb @@ -110,6 +110,16 @@ module ActiveRecord assert_equal 1800, column.type_cast(30.minutes) assert_equal 7200, column.type_cast(2.hours) end + + def test_string_to_time_with_timezone + old = ActiveRecord::Base.default_timezone + [:utc, :local].each do |zone| + ActiveRecord::Base.default_timezone = zone + assert_equal Time.utc(2013, 9, 4, 0, 0, 0), Column.string_to_time("Wed, 04 Sep 2013 03:00:00 EAT") + end + rescue + ActiveRecord::Base.default_timezone = old + end end end end |