Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #6245 from bogdan/bc_timestamp

Postgresql adapter: fix handling of BC timestamps
  • Loading branch information...
commit 1c0e7ab2f8227598cd605e3cee74e16d66c17c43 2 parents 6b266c2 + fa73cf7
@rafaelfranca rafaelfranca authored
View
6 activerecord/CHANGELOG.md
@@ -1,5 +1,11 @@
## Rails 4.0.0 (unreleased) ##
+* Fix postgresql adapter to handle BC timestamps correctly
+
+ HistoryEvent.create!(:name => "something", :occured_at => Date.new(0) - 5.years)
+
+ *Bogdan Gusiev*
+
* When running migrations on Postgresql, the `:limit` option for `binary` and `text` columns is silently dropped.
Previously, these migrations caused sql exceptions, because Postgresql doesn't support limits on these types.
View
2  activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
@@ -8,6 +8,8 @@ def string_to_time(string)
case string
when 'infinity'; 1.0 / 0.0
when '-infinity'; -1.0 / 0.0
+ when / BC$/
+ super("-" + string.sub(/ BC$/, ""))
else
super
end
View
10 activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
@@ -129,11 +129,15 @@ def quote_column_name(name) #:nodoc:
# Quote date/time values for use in SQL input. Includes microseconds
# if the value is a Time responding to usec.
def quoted_date(value) #:nodoc:
+ result = super
if value.acts_like?(:time) && value.respond_to?(:usec)
- "#{super}.#{sprintf("%06d", value.usec)}"
- else
- super
+ result = "#{result}.#{sprintf("%06d", value.usec)}"
+ end
+
+ if value.year < 0
+ result = result.sub(/^-/, "") + " BC"
end
+ result
end
end
end
View
9 activerecord/test/cases/adapters/postgresql/timestamp_test.rb
@@ -75,6 +75,15 @@ def test_postgres_agrees_with_activerecord_about_precision
assert_equal '4', pg_datetime_precision('foos', 'updated_at')
end
+ def test_bc_timestamp
+ unless current_adapter?(:PostgreSQLAdapter)
+ return skip("only tested on postgresql")
+ end
+ date = Date.new(0) - 1.second
+ Developer.create!(:name => "aaron", :updated_at => date)
+ assert_equal date, Developer.find_by_name("aaron").updated_at
+ end
+
private
def pg_datetime_precision(table_name, column_name)
Please sign in to comment.
Something went wrong with that request. Please try again.