From 42775686d2b32b66c971c0560ba2497c7f8703b2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 25 Jun 2006 18:04:06 +0000 Subject: PostgreSQL: support microsecond time resolution. Closes #5492. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4494 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../abstract/schema_definitions.rb | 20 +++++++++++++++----- .../connection_adapters/postgresql_adapter.rb | 6 +++++- 2 files changed, 20 insertions(+), 6 deletions(-) (limited to 'activerecord/lib') 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 fde561d03a..3031c11267 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -1,4 +1,4 @@ -require 'parsedate' +require 'date' module ActiveRecord module ConnectionAdapters #:nodoc: @@ -109,7 +109,9 @@ module ActiveRecord def self.string_to_time(string) return string unless string.is_a?(String) - time_array = ParseDate.parsedate(string)[0..5] + time_hash = Date._parse(string) + time_hash[:sec_fraction] = microseconds(time_hash) + time_array = time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction) # treat 0000-00-00 00:00:00 as nil Time.send(Base.default_timezone, *time_array) rescue nil end @@ -117,9 +119,11 @@ module ActiveRecord def self.string_to_dummy_time(string) return string unless string.is_a?(String) return nil if string.empty? - time_array = ParseDate.parsedate(string) + time_hash = Date._parse(string) + time_hash[:sec_fraction] = microseconds(time_hash) # pad the resulting array with dummy date information - time_array[0] = 2000; time_array[1] = 1; time_array[2] = 1; + time_array = [2000, 1, 1] + time_array += time_hash.values_at(:hour, :min, :sec, :sec_fraction) Time.send(Base.default_timezone, *time_array) rescue nil end @@ -132,7 +136,13 @@ module ActiveRecord end end - private + private + # '0.123456' -> 123456 + # '1.123456' -> 123456 + def self.microseconds(time) + ((time[:sec_fraction].to_f % 1) * 1_000_000).to_i + end + def extract_limit(sql_type) return unless sql_type $1.to_i if sql_type =~ /\((.*)\)/ diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index f46217d4f6..b307ad2fe0 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -124,6 +124,10 @@ module ActiveRecord %("#{name}") end + def quoted_date(value) + value.strftime("%Y-%m-%d %H:%M:%S.#{value.usec}") + end + # DATABASE STATEMENTS ====================================== @@ -526,7 +530,7 @@ module ActiveRecord def cast_to_time(value) return value unless value.class == DateTime v = value - time_array = [v.year, v.month, v.day, v.hour, v.min, v.sec] + time_array = [v.year, v.month, v.day, v.hour, v.min, v.sec, v.usec] Time.send(Base.default_timezone, *time_array) rescue nil end end -- cgit v1.2.3