Format the time string according to the precision of the time column

It is also necessary to format a time column like a datetime column.
This commit is contained in:
Ryuta Kamizono 2015-02-13 03:04:47 +09:00
parent 9ef870c042
commit a088ee9691
7 changed files with 45 additions and 22 deletions

View File

@ -87,7 +87,7 @@ platforms :ruby do
group :db do
gem 'pg', '>= 0.18.0'
gem 'mysql', '>= 2.9.0'
gem 'mysql2', '>= 0.3.13'
gem 'mysql2', '>= 0.3.18'
end
end

View File

@ -253,7 +253,7 @@ DEPENDENCIES
minitest (< 5.3.4)
mocha (~> 0.14)
mysql (>= 2.9.0)
mysql2 (>= 0.3.13)
mysql2 (>= 0.3.18)
nokogiri (>= 1.4.5)
pg (>= 0.18.0)
psych (~> 2.0)

View File

@ -1,3 +1,7 @@
* Format the time string according to the precision of the time column.
*Ryuta Kamizono*
* Allow `:precision` option for time type columns.
*Ryuta Kamizono*

View File

@ -75,7 +75,7 @@ module ActiveRecord
def prepare_column_options(column)
spec = super
spec.delete(:precision) if column.type == :datetime && column.precision == 0
spec.delete(:precision) if /time/ === column.sql_type && column.precision == 0
spec
end
@ -733,7 +733,7 @@ module ActiveRecord
end
def extract_precision(sql_type)
if /datetime/ === sql_type
if /time/ === sql_type
super || 0
else
super

View File

@ -10,24 +10,6 @@ module ActiveRecord
:datetime
end
def serialize(value)
if precision && value.respond_to?(:usec)
number_of_insignificant_digits = 6 - precision
round_power = 10 ** number_of_insignificant_digits
value = value.change(usec: value.usec / round_power * round_power)
end
if value.acts_like?(:time)
zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal
if value.respond_to?(zone_conversion_method)
value = value.send(zone_conversion_method)
end
end
value
end
private
def cast_value(string)

View File

@ -2,6 +2,24 @@ module ActiveRecord
module Type
module Helpers
module TimeValue # :nodoc:
def serialize(value)
if precision && value.respond_to?(:usec)
number_of_insignificant_digits = 6 - precision
round_power = 10 ** number_of_insignificant_digits
value = value.change(usec: value.usec / round_power * round_power)
end
if value.acts_like?(:time)
zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal
if value.respond_to?(zone_conversion_method)
value = value.send(zone_conversion_method)
end
end
value
end
def type_cast_for_schema(value)
"'#{value.to_s(:db)}'"
end

View File

@ -2,6 +2,10 @@ require 'cases/helper'
if ActiveRecord::Base.connection.supports_datetime_with_precision?
class TimePrecisionTest < ActiveRecord::TestCase
self.use_transactional_fixtures = false
class Foo < ActiveRecord::Base; end
setup do
@connection = ActiveRecord::Base.connection
end
@ -45,6 +49,21 @@ class TimePrecisionTest < ActiveRecord::TestCase
assert_equal 4, database_datetime_precision('foos', 'finish')
end
def test_formatting_time_according_to_precision
@connection.create_table(:foos, force: true) do |t|
t.time :start, precision: 0
t.time :finish, precision: 4
end
time = ::Time.utc(2000, 1, 1, 12, 30, 0, 999999)
Foo.create!(start: time, finish: time)
assert foo = Foo.find_by(start: time)
assert_equal 1, Foo.where(finish: time).count
assert_equal time.to_s, foo.start.to_s
assert_equal time.to_s, foo.finish.to_s
assert_equal 000000, foo.start.usec
assert_equal 999900, foo.finish.usec
end
private
def database_datetime_precision(table_name, column_name)