1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix behavior of handling BC era dates.

BC era year is (astronomical year + 1) and starts from 1 BC.
This commit is contained in:
edogawaconan 2014-06-05 18:31:39 +09:00
parent bb8b214184
commit f4bd67b687
3 changed files with 17 additions and 3 deletions

View file

@ -11,7 +11,8 @@ module ActiveRecord
when 'infinity' then ::Float::INFINITY when 'infinity' then ::Float::INFINITY
when '-infinity' then -::Float::INFINITY when '-infinity' then -::Float::INFINITY
when / BC$/ when / BC$/
super("-" + value.sub(/ BC$/, "")) astronomical_year = format("%04d", -value[/^\d+/].to_i + 1)
super(value.sub(/ BC$/, "").sub(/^\d+/, astronomical_year))
else else
super super
end end

View file

@ -149,8 +149,9 @@ module ActiveRecord
result = "#{result}.#{sprintf("%06d", value.usec)}" result = "#{result}.#{sprintf("%06d", value.usec)}"
end end
if value.year < 0 if value.year <= 0
result = result.sub(/^-/, "") + " BC" bce_year = format("%04d", -value.year + 1)
result = result.sub(/^-?\d+/, bce_year) + " BC"
end end
result result
end end

View file

@ -123,6 +123,18 @@ class TimestampTest < ActiveRecord::TestCase
assert_equal date, Developer.find_by_name("aaron").updated_at assert_equal date, Developer.find_by_name("aaron").updated_at
end end
def test_bc_timestamp_leap_year
date = Time.utc(-4, 2, 29)
Developer.create!(:name => "taihou", :updated_at => date)
assert_equal date, Developer.find_by_name("taihou").updated_at
end
def test_bc_timestamp_year_zero
date = Time.utc(0, 4, 7)
Developer.create!(:name => "yahagi", :updated_at => date)
assert_equal date, Developer.find_by_name("yahagi").updated_at
end
private private
def pg_datetime_precision(table_name, column_name) def pg_datetime_precision(table_name, column_name)