aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/type/date_time.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2015-02-01 13:02:56 +0900
committerRyuta Kamizono <kamipo@gmail.com>2015-02-08 22:22:55 +0900
commitf9839120379292cbc5da25f35311f457b08b44a8 (patch)
tree32d780f568088bbfa3519a0f8ca23137ca7d533a /activerecord/lib/active_record/type/date_time.rb
parent31fafe0a3223b58e214c03c88a33d6ef4435f63c (diff)
downloadrails-f9839120379292cbc5da25f35311f457b08b44a8.tar.gz
rails-f9839120379292cbc5da25f35311f457b08b44a8.tar.bz2
rails-f9839120379292cbc5da25f35311f457b08b44a8.zip
Fix rounding problem for PostgreSQL timestamp column
If timestamp column have the precision, it need to format according to the precision of timestamp column.
Diffstat (limited to 'activerecord/lib/active_record/type/date_time.rb')
-rw-r--r--activerecord/lib/active_record/type/date_time.rb21
1 files changed, 14 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/type/date_time.rb b/activerecord/lib/active_record/type/date_time.rb
index 5646ee61db..e8614b16e0 100644
--- a/activerecord/lib/active_record/type/date_time.rb
+++ b/activerecord/lib/active_record/type/date_time.rb
@@ -11,21 +11,28 @@ module ActiveRecord
end
def type_cast_for_database(value)
+ return super unless value.acts_like?(:time)
+
zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal
- if value.acts_like?(:time)
- if value.respond_to?(zone_conversion_method)
- value.send(zone_conversion_method)
- else
- value
- end
+ if value.respond_to?(zone_conversion_method)
+ value = value.send(zone_conversion_method)
+ end
+
+ return value unless has_precision?
+
+ result = value.to_s(:db)
+ if value.respond_to?(:usec) && (1..6).cover?(precision)
+ "#{result}.#{sprintf("%0#{precision}d", value.usec / 10 ** (6 - precision))}"
else
- super
+ result
end
end
private
+ alias has_precision? precision
+
def cast_value(string)
return string unless string.is_a?(::String)
return if string.empty?