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

PostgreSQL: Allow BC dates like datetime consistently

BC dates are supported by both date and datetime types.

https://www.postgresql.org/docs/current/static/datatype-datetime.html

Since #1097, new datetime allows year zero as 1 BC, but new date does
not. It should be allowed even in new date consistently.
This commit is contained in:
Ryuta Kamizono 2018-02-20 23:38:06 +09:00
parent 5d78256ee3
commit cfbde022ee
3 changed files with 22 additions and 1 deletions

View file

@ -42,7 +42,7 @@ module ActiveModel
end
def new_date(year, mon, mday)
if year && year != 0
unless year.nil? || (year == 0 && mon == 0 && mday == 0)
::Date.new(year, mon, mday) rescue nil
end
end

View file

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

View file

@ -21,4 +21,22 @@ class PostgresqlDateTest < ActiveRecord::PostgreSQLTestCase
topic = Topic.create!(last_read: -1.0 / 0.0)
assert_equal(-1.0 / 0.0, topic.last_read)
end
def test_bc_date
date = Date.new(0) - 1.week
topic = Topic.create!(last_read: date)
assert_equal date, Topic.find(topic.id).last_read
end
def test_bc_date_leap_year
date = Time.utc(-4, 2, 29).to_date
topic = Topic.create!(last_read: date)
assert_equal date, Topic.find(topic.id).last_read
end
def test_bc_date_year_zero
date = Time.utc(0, 4, 7).to_date
topic = Topic.create!(last_read: date)
assert_equal date, Topic.find(topic.id).last_read
end
end