aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorkennyj <kennyj@gmail.com>2012-07-06 02:59:47 +0900
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-08-21 10:12:13 -0300
commite9d2ad395ec2ef929d74752f3d71c80674044fbe (patch)
treedd286d1791f70d7d6f245531ade77101c528919d /activerecord/lib
parent15e2b80a27c7fd48f1a3e22d1682281515b2c371 (diff)
downloadrails-e9d2ad395ec2ef929d74752f3d71c80674044fbe.tar.gz
rails-e9d2ad395ec2ef929d74752f3d71c80674044fbe.tar.bz2
rails-e9d2ad395ec2ef929d74752f3d71c80674044fbe.zip
Round usec when writing timestamp attribute.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb19
1 files changed, 14 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
index fa5b2ef336..d1e9d2de0e 100644
--- a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
+++ b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
@@ -59,11 +59,14 @@ module ActiveRecord
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 = round_usec(zoned_time)
+ rounded_value = round_usec(read_attribute("#{attr_name}"))
+ if (rounded_value != rounded_time) || (!rounded_value && 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 @@ module ActiveRecord
[:datetime, :timestamp].include?(column.type)
end
end
+
+ private
+ def round_usec(value)
+ return unless value
+ value.change(:usec => 0)
+ end
end
end
end