diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-10-07 22:01:59 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-10-07 22:01:59 +0000 |
commit | bd087e6716f8b48d48386c3d42e529849d7b68cd (patch) | |
tree | 1db81d7c2f6112f33db9f6929065055980dc3a59 /activerecord/lib/active_record/connection_adapters/abstract | |
parent | 459cc1ecb84aea273ecf8183fb791f61e26c19b0 (diff) | |
download | rails-bd087e6716f8b48d48386c3d42e529849d7b68cd.tar.gz rails-bd087e6716f8b48d48386c3d42e529849d7b68cd.tar.bz2 rails-bd087e6716f8b48d48386c3d42e529849d7b68cd.zip |
Use fast date/time parsing by default. Closes #9811.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7780 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index 507ddd2499..0325bdbbd8 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -6,6 +6,11 @@ module ActiveRecord module ConnectionAdapters #:nodoc: # An abstract definition of a column in a table. class Column + module Format + ISO_DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/ + ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/ + end + attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale attr_accessor :primary @@ -109,18 +114,16 @@ module ActiveRecord def string_to_date(string) return string unless string.is_a?(String) + return nil if string.empty? - new_date *ParseDate.parsedate(string)[0..2] + fast_string_to_date(string) || fallback_string_to_date(string) end def string_to_time(string) return string unless string.is_a?(String) return nil if string.empty? - 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) + fast_string_to_time(string) || fallback_string_to_time(string) end def string_to_dummy_time(string) @@ -174,6 +177,31 @@ module ActiveRecord # Append zero calendar reform start to account for dates skipped by calendar reform DateTime.new(year, mon, mday, hour, min, sec, zone_offset, 0) rescue nil end + + def fast_string_to_date(string) + if string =~ Format::ISO_DATE + new_date $1.to_i, $2.to_i, $3.to_i + end + end + + # Doesn't handle time zones. + def fast_string_to_time(string) + if string =~ Format::ISO_DATETIME + microsec = ($7.to_f * 10e6).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 + + def fallback_string_to_date(string) + new_date *ParseDate.parsedate(string)[0..2] + 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) + end end private |