Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Round usec when writing timestamp attribute.

  • Loading branch information...
commit cd66946ad3a04153ec7e876256e726ace97542ba 1 parent 15e2b80
@kennyj authored
View
19 activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
@@ -59,11 +59,14 @@ def #{attr_name}=(original_time)
unless time.acts_like?(:time)
time = time.is_a?(String) ? Time.zone.parse(time) : time.to_time rescue time
end
- time = time.in_time_zone rescue nil if time
- changed = read_attribute(:#{attr_name}) != time
- write_attribute(:#{attr_name}, original_time)
- #{attr_name}_will_change! if changed
- @attributes_cache["#{attr_name}"] = time
+ zoned_time = time && time.in_time_zone rescue nil
+ rounded_time = rounded_current_value(zoned_time)
+ attr = rounded_current_value(read_attribute("#{attr_name}"))
+ if (attr != rounded_time) || (!attr && original_time)
+ write_attribute("#{attr_name}", original_time)
+ #{attr_name}_will_change!
+ @attributes_cache["#{attr_name}"] = zoned_time
+ end
end
EOV
generated_attribute_methods.module_eval(method_body, __FILE__, line)
@@ -79,6 +82,12 @@ def create_time_zone_conversion_attribute?(name, column)
[:datetime, :timestamp].include?(column.type)
end
end
+
+ private
+ def rounded_current_value(value)
+ return unless value
+ value.change(:usec => 0)
+ end
end
end
end
View
15 activerecord/test/cases/dirty_test.rb
@@ -525,6 +525,21 @@ def test_field_named_field
end
end
+ def test_setting_time_attributes_with_time_zone_field_to_same_time_should_not_be_marked_as_a_change
+ in_time_zone 'Paris' do
+ target = Class.new(ActiveRecord::Base)
+ target.table_name = 'pirates'
+
+ created_on = Time.now
+
+ pirate = target.create(:created_on => created_on)
+ pirate.reload # Here mysql truncate the usec value to 0
+
+ pirate.created_on = created_on
+ assert !pirate.created_on_changed?
+ end
+ end
+
private
def with_partial_updates(klass, on = true)
old = klass.partial_updates?

0 comments on commit cd66946

Please sign in to comment.
Something went wrong with that request. Please try again.