From 572aa0bb1ebe21360635f2353d20ccde55362b4a Mon Sep 17 00:00:00 2001
From: Jacob Lauemoeller <jacob.lauemoeller@iteray.com>
Date: Thu, 14 Jan 2010 14:03:13 +0100
Subject: [PATCH 1/2] Fixed microsecond conversion bug

---
 .../abstract/schema_definitions.rb                 |    4 ++--
 1 files changed, 2 insertions(+), 2 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 520f3c8..4d61124 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -170,7 +170,7 @@ module ActiveRecord
           # '0.123456' -> 123456
           # '1.123456' -> 123456
           def microseconds(time)
-            ((time[:sec_fraction].to_f % 1) * 1_000_000).to_i
+            ((time[:sec_fraction].to_f % 1) * 1_000_000).round
           end
 
           def new_date(year, mon, mday)
@@ -195,7 +195,7 @@ module ActiveRecord
           # Doesn't handle time zones.
           def fast_string_to_time(string)
             if string =~ Format::ISO_DATETIME
-              microsec = ($7.to_f * 1_000_000).to_i
+              microsec = ($7.to_f * 1_000_000).round
               new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
             end
           end
-- 
1.6.4.2


From 4580ce83234f6d0fb0553f3c8437387c47ac7e78 Mon Sep 17 00:00:00 2001
From: Jacob Lauemoeller <jacob.lauemoeller@iteray.com>
Date: Thu, 14 Jan 2010 16:08:10 +0100
Subject: [PATCH 2/2] Fixed microsecond conversion bug

---
 activerecord/test/cases/schema_definitions_test.rb |   23 ++++++++++++++++++++
 1 files changed, 23 insertions(+), 0 deletions(-)
 create mode 100644 activerecord/test/cases/schema_definitions_test.rb

diff --git a/activerecord/test/cases/schema_definitions_test.rb b/activerecord/test/cases/schema_definitions_test.rb
new file mode 100644
index 0000000..622a93a
--- /dev/null
+++ b/activerecord/test/cases/schema_definitions_test.rb
@@ -0,0 +1,23 @@
+require "cases/helper"
+
+class SchemaDefinitionsTest < ActiveRecord::TestCase
+
+  REGRESSION_SAMPLES = %w{000249 125014 003912 256051 524287}
+  
+  test "fast_string_to_time can handle problematic microseconds" do
+    REGRESSION_SAMPLES.each do |u|
+      converted = ActiveRecord::ConnectionAdapters::Column.send('fast_string_to_time', "2010-01-12 12:34:56.#{u}")
+      assert_equal u.to_i, converted.usec
+    end
+  end
+
+  test "microseconds can handle problematic microseconds" do
+    REGRESSION_SAMPLES.each do |u|
+      converted = ActiveRecord::ConnectionAdapters::Column.send('microseconds', {:sec_fraction => "0.#{u}"})
+      assert_equal u.to_i, converted
+
+      converted = ActiveRecord::ConnectionAdapters::Column.send('microseconds', {:sec_fraction => "1.#{u}"})
+      assert_equal u.to_i, converted
+    end
+  end
+end
-- 
1.6.4.2

