mirror of https://github.com/rails/rails
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:
parent
9ef870c042
commit
a088ee9691
2
Gemfile
2
Gemfile
|
@ -87,7 +87,7 @@ platforms :ruby do
|
||||||
group :db do
|
group :db do
|
||||||
gem 'pg', '>= 0.18.0'
|
gem 'pg', '>= 0.18.0'
|
||||||
gem 'mysql', '>= 2.9.0'
|
gem 'mysql', '>= 2.9.0'
|
||||||
gem 'mysql2', '>= 0.3.13'
|
gem 'mysql2', '>= 0.3.18'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -253,7 +253,7 @@ DEPENDENCIES
|
||||||
minitest (< 5.3.4)
|
minitest (< 5.3.4)
|
||||||
mocha (~> 0.14)
|
mocha (~> 0.14)
|
||||||
mysql (>= 2.9.0)
|
mysql (>= 2.9.0)
|
||||||
mysql2 (>= 0.3.13)
|
mysql2 (>= 0.3.18)
|
||||||
nokogiri (>= 1.4.5)
|
nokogiri (>= 1.4.5)
|
||||||
pg (>= 0.18.0)
|
pg (>= 0.18.0)
|
||||||
psych (~> 2.0)
|
psych (~> 2.0)
|
||||||
|
|
|
@ -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.
|
* Allow `:precision` option for time type columns.
|
||||||
|
|
||||||
*Ryuta Kamizono*
|
*Ryuta Kamizono*
|
||||||
|
|
|
@ -75,7 +75,7 @@ module ActiveRecord
|
||||||
|
|
||||||
def prepare_column_options(column)
|
def prepare_column_options(column)
|
||||||
spec = super
|
spec = super
|
||||||
spec.delete(:precision) if column.type == :datetime && column.precision == 0
|
spec.delete(:precision) if /time/ === column.sql_type && column.precision == 0
|
||||||
spec
|
spec
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -733,7 +733,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract_precision(sql_type)
|
def extract_precision(sql_type)
|
||||||
if /datetime/ === sql_type
|
if /time/ === sql_type
|
||||||
super || 0
|
super || 0
|
||||||
else
|
else
|
||||||
super
|
super
|
||||||
|
|
|
@ -10,24 +10,6 @@ module ActiveRecord
|
||||||
:datetime
|
:datetime
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def cast_value(string)
|
def cast_value(string)
|
||||||
|
|
|
@ -2,6 +2,24 @@ module ActiveRecord
|
||||||
module Type
|
module Type
|
||||||
module Helpers
|
module Helpers
|
||||||
module TimeValue # :nodoc:
|
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)
|
def type_cast_for_schema(value)
|
||||||
"'#{value.to_s(:db)}'"
|
"'#{value.to_s(:db)}'"
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,6 +2,10 @@ require 'cases/helper'
|
||||||
|
|
||||||
if ActiveRecord::Base.connection.supports_datetime_with_precision?
|
if ActiveRecord::Base.connection.supports_datetime_with_precision?
|
||||||
class TimePrecisionTest < ActiveRecord::TestCase
|
class TimePrecisionTest < ActiveRecord::TestCase
|
||||||
|
self.use_transactional_fixtures = false
|
||||||
|
|
||||||
|
class Foo < ActiveRecord::Base; end
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
@connection = ActiveRecord::Base.connection
|
@connection = ActiveRecord::Base.connection
|
||||||
end
|
end
|
||||||
|
@ -45,6 +49,21 @@ class TimePrecisionTest < ActiveRecord::TestCase
|
||||||
assert_equal 4, database_datetime_precision('foos', 'finish')
|
assert_equal 4, database_datetime_precision('foos', 'finish')
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def database_datetime_precision(table_name, column_name)
|
def database_datetime_precision(table_name, column_name)
|
||||||
|
|
Loading…
Reference in New Issue