Permalink
Browse files

PostgreSQL: support microsecond time resolution. Closes #5492.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4494 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent d912509 commit 42775686d2b32b66c971c0560ba2497c7f8703b2 @jeremy jeremy committed Jun 25, 2006
View
@@ -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]
@@ -1,4 +1,4 @@
-require 'parsedate'
+require 'date'
module ActiveRecord
module ConnectionAdapters #:nodoc:
@@ -109,17 +109,21 @@ def self.string_to_date(string)
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
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 @@ def self.value_to_boolean(value)
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 =~ /\((.*)\)/
@@ -124,6 +124,10 @@ def quote_column_name(name)
%("#{name}")
end
+ def quoted_date(value)
+ value.strftime("%Y-%m-%d %H:%M:%S.#{value.usec}")
+ end
+
# DATABASE STATEMENTS ======================================
@@ -526,7 +530,7 @@ def default_value(value)
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
@@ -315,6 +315,12 @@ def test_preserving_time_objects
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
@@ -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

0 comments on commit 4277568

Please sign in to comment.