diff options
author | Chris Hapgood <cch1@hapgoods.com> | 2010-01-15 18:34:49 -0500 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2010-01-16 15:21:36 +1300 |
commit | 717a2941e15b32d07cc456bb0d81742ecfc5b4a3 (patch) | |
tree | 63ec271188b01be0cb5ab08b7dc156ef4a400ef8 /activerecord/lib | |
parent | 81cd11259c52544dd1bc401b7097e4a0e5d34fe6 (diff) | |
download | rails-717a2941e15b32d07cc456bb0d81742ecfc5b4a3.tar.gz rails-717a2941e15b32d07cc456bb0d81742ecfc5b4a3.tar.bz2 rails-717a2941e15b32d07cc456bb0d81742ecfc5b4a3.zip |
Fix #microseconds conversion and #fast_string_to_time
* Use direct integer parsing in #fast_string_to_time to avoid convoluted
conversions and errors due to truncation.
* Use Float#round in #microseconds to avoid truncation errors.
Signed-off-by: Michael Koziarski <michael@koziarski.com>
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb | 13 |
1 files changed, 7 insertions, 6 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 520f3c8c0c..5e8a01644d 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -13,6 +13,7 @@ module ActiveRecord 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/ + NEW_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 @@ -167,10 +168,11 @@ module ActiveRecord end protected - # '0.123456' -> 123456 - # '1.123456' -> 123456 + # Rational(123456, 1_000_000) -> 123456 + # The sec_fraction component returned by Date._parse is a Rational fraction of a second or nil + # NB: This method is optimized for performance by immediately converting away from Rational. 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) @@ -194,9 +196,8 @@ 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 - new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec + if md = Format::NEW_ISO_DATETIME.match(string) + new_time *md.to_a[1..7].map(&:to_i) end end |