diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-06-25 18:04:06 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-06-25 18:04:06 +0000 |
commit | 42775686d2b32b66c971c0560ba2497c7f8703b2 (patch) | |
tree | 61539c008bc3690abec2349d7d0c32e0567fde44 | |
parent | d9125093e8caaf112312d6e59052c3f20725fad2 (diff) | |
download | rails-42775686d2b32b66c971c0560ba2497c7f8703b2.tar.gz rails-42775686d2b32b66c971c0560ba2497c7f8703b2.tar.bz2 rails-42775686d2b32b66c971c0560ba2497c7f8703b2.zip |
PostgreSQL: support microsecond time resolution. Closes #5492.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4494 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
5 files changed, 29 insertions, 7 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index ccce8c44c5..c7779b6efa 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* PostgreSQL: support microsecond time resolution. #5492 [alex@msgpad.com] + * Add AssociationCollection#sum since the method_missing invokation has been shadowed by Enumerable#sum. * Added find_or_initialize_by_X which works like find_or_create_by_X but doesn't save the newly instantiated record. [Sam Stephenson] 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 diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index 2cbc2cfd2b..15605d779a 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -315,6 +315,12 @@ class BasicsTest < Test::Unit::TestCase Time, Topic.find(1).written_on, "The written_on attribute should be of the Time class" ) + + # For adapters which support microsecond resolution. + if current_adapter?(:PostgreSQLAdapter) + assert_equal 11, Topic.find(1).written_on.sec + assert_equal 223300, Topic.find(1).written_on.usec + end end def test_destroy diff --git a/activerecord/test/fixtures/topics.yml b/activerecord/test/fixtures/topics.yml index b1e5159be2..e78c0e1b3d 100644 --- a/activerecord/test/fixtures/topics.yml +++ b/activerecord/test/fixtures/topics.yml @@ -3,7 +3,7 @@ first: title: The First Topic author_name: David author_email_address: david@loudthinking.com - written_on: 2003-07-16t15:28:00.00+01:00 + written_on: 2003-07-16t15:28:11.2233+01:00 last_read: 2004-04-15 bonus_time: 2005-01-30t15:28:00.00+01:00 content: Have a nice day |