Skip to content

Commit

Permalink
Match String#to_time's behaviour to ruby
Browse files Browse the repository at this point in the history
Previously `String#to_time` returned the midnight of the current date
in some cases where there was no relavant information in the string.
Now the method returns `nil` instead in those cases.

Fixes #22958.
  • Loading branch information
Siim Liiser authored and pixeltrix committed Apr 3, 2016
1 parent 0807312 commit 9c2c677
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
11 changes: 11 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,12 @@
* Match `String#to_time`'s behaviour to that of ruby's implementation for edge cases.

`nil` is now returned instead of the current date if the string provided does
contain time information, but none that is used to build the `Time` object.

Fixes #22958.

*Siim Liiser*

* Rely on the native DateTime#<=> implementation to handle non-datetime like * Rely on the native DateTime#<=> implementation to handle non-datetime like
objects instead of returning `nil` ourselves. This restores the ability objects instead of returning `nil` ourselves. This restores the ability
of `DateTime` instances to be compared with a `Numeric` that represents an of `DateTime` instances to be compared with a `Numeric` that represents an
Expand Down Expand Up @@ -57,6 +66,7 @@


*Jon Moss* *Jon Moss*



## Rails 5.0.0.beta2 (February 01, 2016) ## ## Rails 5.0.0.beta2 (February 01, 2016) ##


* Change `number_to_currency` behavior for checking negativity. * Change `number_to_currency` behavior for checking negativity.
Expand Down Expand Up @@ -94,6 +104,7 @@


*Akshay Vishnoi* *Akshay Vishnoi*



## Rails 5.0.0.beta1 (December 18, 2015) ## ## Rails 5.0.0.beta1 (December 18, 2015) ##


* Add thread_m/cattr_accessor/reader/writer suite of methods for declaring class and module variables that live per-thread. * Add thread_m/cattr_accessor/reader/writer suite of methods for declaring class and module variables that live per-thread.
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class String
# "12/13/2012".to_time # => ArgumentError: argument out of range # "12/13/2012".to_time # => ArgumentError: argument out of range
def to_time(form = :local) def to_time(form = :local)
parts = Date._parse(self, false) parts = Date._parse(self, false)
return if parts.empty? used_keys = %i(year mon mday hour min sec sec_fraction offset)
return if (parts.keys & used_keys).empty?


now = Time.now now = Time.now
time = Time.new( time = Time.new(
Expand Down
1 change: 1 addition & 0 deletions activesupport/test/core_ext/string_ext_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ def test_string_to_time
assert_equal Time.local(2011, 2, 27, 17, 50), "2011-02-27 13:50 -0100".to_time assert_equal Time.local(2011, 2, 27, 17, 50), "2011-02-27 13:50 -0100".to_time
assert_equal Time.utc(2011, 2, 27, 23, 50), "2011-02-27 22:50 -0100".to_time(:utc) assert_equal Time.utc(2011, 2, 27, 23, 50), "2011-02-27 22:50 -0100".to_time(:utc)
assert_equal Time.local(2005, 2, 27, 22, 50), "2005-02-27 14:50 -0500".to_time assert_equal Time.local(2005, 2, 27, 22, 50), "2005-02-27 14:50 -0500".to_time
assert_nil "010".to_time
assert_nil "".to_time assert_nil "".to_time
end end
end end
Expand Down

0 comments on commit 9c2c677

Please sign in to comment.