aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/type/date_time.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-05-20 14:07:58 -0700
committerSean Griffin <sean@thoughtbot.com>2014-05-20 14:07:58 -0700
commit8f387995054d9ce79522da8ef65fba28c6ea0785 (patch)
tree53d38918ff2ccbb11dd503d56108e21df031646c /activerecord/lib/active_record/connection_adapters/type/date_time.rb
parent25c672637206a2c48fd829c58596c788b6e31c5d (diff)
downloadrails-8f387995054d9ce79522da8ef65fba28c6ea0785.tar.gz
rails-8f387995054d9ce79522da8ef65fba28c6ea0785.tar.bz2
rails-8f387995054d9ce79522da8ef65fba28c6ea0785.zip
Inline typecasting helpers from Column to the appropriate types
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/type/date_time.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/type/date_time.rb18
1 files changed, 17 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/type/date_time.rb b/activerecord/lib/active_record/connection_adapters/type/date_time.rb
index 757553cd68..c34f4c5a53 100644
--- a/activerecord/lib/active_record/connection_adapters/type/date_time.rb
+++ b/activerecord/lib/active_record/connection_adapters/type/date_time.rb
@@ -11,7 +11,23 @@ module ActiveRecord
private
def cast_value(string)
- Column.string_to_time(string)
+ return string unless string.is_a?(::String)
+ return if string.empty?
+
+ fast_string_to_time(string) || fallback_string_to_time(string)
+ end
+
+ # '0.123456' -> 123456
+ # '1.123456' -> 123456
+ def microseconds(time)
+ time[:sec_fraction] ? (time[:sec_fraction] * 1_000_000).to_i : 0
+ end
+
+ def fallback_string_to_time(string)
+ 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, :offset))
end
end
end