diff options
author | Scott Fleckenstein <nullstyle@gmail.com> | 2008-05-07 06:54:07 -0700 |
---|---|---|
committer | gbuesing <gbuesing@gmail.com> | 2008-05-08 19:25:31 -0500 |
commit | eb5b93be74ed3eca925c1ab9bd4739919ae39a41 (patch) | |
tree | 47e40b6cdb3a13911d2c3ff75761b4e7c1e9775a /activerecord | |
parent | bcb090c56b842a76397e0ea32f54c942fd11910e (diff) | |
download | rails-eb5b93be74ed3eca925c1ab9bd4739919ae39a41.tar.gz rails-eb5b93be74ed3eca925c1ab9bd4739919ae39a41.tar.bz2 rails-eb5b93be74ed3eca925c1ab9bd4739919ae39a41.zip |
Fix Time.zone.parse from stripping time zone information and make Time aware attribute methods use Time.zone.parse instead of to_time
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/attribute_methods.rb | 2 | ||||
-rwxr-xr-x | activerecord/test/cases/attribute_methods_test.rb | 27 |
2 files changed, 28 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 46ecfc1969..973b761546 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -180,7 +180,7 @@ module ActiveRecord method_body = <<-EOV def #{attr_name}=(time) if time - time = time.to_time rescue time unless time.acts_like?(:time) + time = Time.zone.parse(time) rescue time unless time.acts_like?(:time) time = time.in_time_zone if time.acts_like?(:time) end write_attribute(:#{attr_name}, time) diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 61a049ab36..ab9abf27f9 100755 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -173,6 +173,33 @@ class AttributeMethodsTest < ActiveRecord::TestCase end end + def test_setting_time_zone_aware_attribute_with_string + utc_time = Time.utc(2008, 1, 1) + (-11..13).each do |timezone_offset| + time_string = utc_time.in_time_zone(timezone_offset).to_s + in_time_zone "Pacific Time (US & Canada)" do + record = @target.new + record.written_on = time_string + assert_equal Time.zone.parse(time_string), record.written_on + assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone + assert_equal Time.utc(2007, 12, 31, 16), record.written_on.time + end + end + end + + def test_setting_time_zone_aware_attribute_interprets_time_zone_unaware_string_in_time_zone + time_string = 'Tue Jan 01 00:00:00 2008' + (-11..13).each do |timezone_offset| + in_time_zone timezone_offset do + record = @target.new + record.written_on = time_string + assert_equal Time.zone.parse(time_string), record.written_on + assert_equal TimeZone[timezone_offset], record.written_on.time_zone + assert_equal Time.utc(2008, 1, 1), record.written_on.time + end + end + end + def test_setting_time_zone_aware_attribute_in_current_time_zone utc_time = Time.utc(2008, 1, 1) in_time_zone "Pacific Time (US & Canada)" do |