Skip to content

Commit 1c0e7ab

Browse files
committed
Merge pull request rails#6245 from bogdan/bc_timestamp
Postgresql adapter: fix handling of BC timestamps
2 parents 6b266c2 + fa73cf7 commit 1c0e7ab

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

activerecord/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## Rails 4.0.0 (unreleased) ##
22

3+
* Fix postgresql adapter to handle BC timestamps correctly
4+
5+
HistoryEvent.create!(:name => "something", :occured_at => Date.new(0) - 5.years)
6+
7+
*Bogdan Gusiev*
8+
39
* When running migrations on Postgresql, the `:limit` option for `binary` and `text` columns is silently dropped.
410
Previously, these migrations caused sql exceptions, because Postgresql doesn't support limits on these types.
511

activerecord/lib/active_record/connection_adapters/postgresql/cast.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ def string_to_time(string)
88
case string
99
when 'infinity'; 1.0 / 0.0
1010
when '-infinity'; -1.0 / 0.0
11+
when / BC$/
12+
super("-" + string.sub(/ BC$/, ""))
1113
else
1214
super
1315
end

activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,15 @@ def quote_column_name(name) #:nodoc:
129129
# Quote date/time values for use in SQL input. Includes microseconds
130130
# if the value is a Time responding to usec.
131131
def quoted_date(value) #:nodoc:
132+
result = super
132133
if value.acts_like?(:time) && value.respond_to?(:usec)
133-
"#{super}.#{sprintf("%06d", value.usec)}"
134-
else
135-
super
134+
result = "#{result}.#{sprintf("%06d", value.usec)}"
135+
end
136+
137+
if value.year < 0
138+
result = result.sub(/^-/, "") + " BC"
136139
end
140+
result
137141
end
138142
end
139143
end

activerecord/test/cases/adapters/postgresql/timestamp_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ def test_postgres_agrees_with_activerecord_about_precision
7575
assert_equal '4', pg_datetime_precision('foos', 'updated_at')
7676
end
7777

78+
def test_bc_timestamp
79+
unless current_adapter?(:PostgreSQLAdapter)
80+
return skip("only tested on postgresql")
81+
end
82+
date = Date.new(0) - 1.second
83+
Developer.create!(:name => "aaron", :updated_at => date)
84+
assert_equal date, Developer.find_by_name("aaron").updated_at
85+
end
86+
7887
private
7988

8089
def pg_datetime_precision(table_name, column_name)

0 commit comments

Comments
 (0)