Permalink
Browse files

Merge pull request #15521 from edogawaconan/fix_bc_postgres_master

Fix BC year handling in postgres
  • Loading branch information...
matthewd committed Jun 5, 2014
2 parents 1c39310 + f4bd67b commit e496a25251dc9c1b455bf9e97baaf58b0e99840b
@@ -11,7 +11,8 @@ def cast_value(value)
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 @@ def quoted_date(value) #:nodoc:
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 @@ def test_bc_timestamp
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)

0 comments on commit e496a25

Please sign in to comment.