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:
parent
bb8b214184
commit
f4bd67b687
3 changed files with 17 additions and 3 deletions
|
@ -11,7 +11,8 @@ module ActiveRecord
|
|||
when 'infinity' then ::Float::INFINITY
|
||||
when '-infinity' then -::Float::INFINITY
|
||||
when / BC$/
|
||||
super("-" + value.sub(/ BC$/, ""))
|
||||
astronomical_year = format("%04d", -value[/^\d+/].to_i + 1)
|
||||
super(value.sub(/ BC$/, "").sub(/^\d+/, astronomical_year))
|
||||
else
|
||||
super
|
||||
end
|
||||
|
|
|
@ -149,8 +149,9 @@ module ActiveRecord
|
|||
result = "#{result}.#{sprintf("%06d", value.usec)}"
|
||||
end
|
||||
|
||||
if value.year < 0
|
||||
result = result.sub(/^-/, "") + " BC"
|
||||
if value.year <= 0
|
||||
bce_year = format("%04d", -value.year + 1)
|
||||
result = result.sub(/^-?\d+/, bce_year) + " BC"
|
||||
end
|
||||
result
|
||||
end
|
||||
|
|
|
@ -123,6 +123,18 @@ class TimestampTest < ActiveRecord::TestCase
|
|||
assert_equal date, Developer.find_by_name("aaron").updated_at
|
||||
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
|
||||
|
||||
def pg_datetime_precision(table_name, column_name)
|
||||
|
|
Loading…
Reference in a new issue