Skip to content

Commit

Permalink
Merge pull request rails#7337 from adzap/string_to_dummy_time
Browse files Browse the repository at this point in the history
Fix for time type columns with invalid time value
Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information
rafaelfranca committed Sep 5, 2012
1 parent 87ac5b4 commit 5054e26
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,9 @@
## Rails 3.2.9 (unreleased)

* Fix time column type casting for invalid time string values to correctly return nil.

*Adam Meehan*

* Fix `becomes` when using a configured `inheritance_column`.

*Yves Senn*
Expand Down
8 changes: 7 additions & 1 deletion activerecord/lib/active_record/connection_adapters/column.rb
Expand Up @@ -150,7 +150,13 @@ def string_to_dummy_time(string)
return string unless string.is_a?(String)
return nil if string.empty?

string_to_time "2000-01-01 #{string}"
dummy_time_string = "2000-01-01 #{string}"

fast_string_to_time(dummy_time_string) || begin
time_hash = Date._parse(dummy_time_string)
return nil if time_hash[:hour].nil?
new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction))
end
end

# convert something to a boolean
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -919,6 +919,18 @@ def test_attributes_on_dummy_time
assert_equal Time.local(2000, 1, 1, 5, 42, 0), topic.bonus_time
end

def test_attributes_on_dummy_time_with_invalid_time
# Oracle, and Sybase do not have a TIME datatype.
return true if current_adapter?(:OracleAdapter, :SybaseAdapter)

attributes = {
"bonus_time" => "not a time"
}
topic = Topic.find(1)
topic.attributes = attributes
assert_nil topic.bonus_time
end

def test_boolean
b_nil = Boolean.create({ "value" => nil })
nil_id = b_nil.id
Expand Down

0 comments on commit 5054e26

Please sign in to comment.