aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/type/time_value.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-05-27 10:59:22 -0700
committerSean Griffin <sean@thoughtbot.com>2014-05-27 19:44:29 -0700
commit728fa69839d2c3b839391f9077896c06df80bddf (patch)
treedab29e5d69868c8abf9a00491ca8cc0acd5d04c3 /activerecord/lib/active_record/type/time_value.rb
parent7f73b9152cfc3f218cfc862e971ba56b94f6be10 (diff)
downloadrails-728fa69839d2c3b839391f9077896c06df80bddf.tar.gz
rails-728fa69839d2c3b839391f9077896c06df80bddf.tar.bz2
rails-728fa69839d2c3b839391f9077896c06df80bddf.zip
Move types to the top level `ActiveRecord` namespace
`ActiveRecord::ConnectionAdapters::Type::Value` => `ActiveRecord::Type::Value`
Diffstat (limited to 'activerecord/lib/active_record/type/time_value.rb')
-rw-r--r--activerecord/lib/active_record/type/time_value.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/type/time_value.rb b/activerecord/lib/active_record/type/time_value.rb
new file mode 100644
index 0000000000..6cc19b6379
--- /dev/null
+++ b/activerecord/lib/active_record/type/time_value.rb
@@ -0,0 +1,34 @@
+module ActiveRecord
+ module Type
+ module TimeValue # :nodoc:
+ def klass
+ ::Time
+ end
+
+ private
+
+ def new_time(year, mon, mday, hour, min, sec, microsec, offset = nil)
+ # Treat 0000-00-00 00:00:00 as nil.
+ return if year.nil? || (year == 0 && mon == 0 && mday == 0)
+
+ if offset
+ time = ::Time.utc(year, mon, mday, hour, min, sec, microsec) rescue nil
+ return unless time
+
+ time -= offset
+ Base.default_timezone == :utc ? time : time.getlocal
+ else
+ ::Time.public_send(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
+ end
+ end
+
+ # Doesn't handle time zones.
+ def fast_string_to_time(string)
+ if string =~ ConnectionAdapters::Column::Format::ISO_DATETIME
+ microsec = ($7.to_r * 1_000_000).to_i
+ new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
+ end
+ end
+ end
+ end
+end