Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Time.zone.parse: return nil for strings with no date information

  • Loading branch information...
commit 06a7c2948a8dbf31357b552d468fcf42002736e7 1 parent fb9bf16
Geoff Buesing gbuesing authored
2  activerecord/lib/active_record/attribute_methods.rb
View
@@ -179,7 +179,7 @@ def define_write_method(attr_name)
def define_write_method_for_time_zone_conversion(attr_name)
method_body = <<-EOV
def #{attr_name}=(time)
- unless time.blank? || time.acts_like?(:time)
+ unless time.acts_like?(:time)
time = time.is_a?(String) ? Time.zone.parse(time) : time.to_time rescue time
end
time = time.in_time_zone rescue nil if time
4 activesupport/lib/active_support/values/time_zone.rb
View
@@ -213,8 +213,10 @@ def at(secs)
# Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
# Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
def parse(str, now=now)
+ date_parts = Date._parse(str)
+ return if date_parts.blank?
time = Time.parse(str, now) rescue DateTime.parse(str)
- if Date._parse(str)[:offset].nil?
+ if date_parts[:offset].nil?
ActiveSupport::TimeWithZone.new(nil, self, time)
else
time.in_time_zone(self)
8 activesupport/test/time_zone_test.rb
View
@@ -198,6 +198,14 @@ def test_parse_far_future_date_with_time_zone_offset_in_string
assert_equal zone, twz.time_zone
end
end
+
+ def test_parse_returns_nil_when_string_without_date_information_is_passed_in
+ silence_warnings do # silence warnings raised by tzinfo gem
+ zone = TimeZone['Eastern Time (US & Canada)']
+ assert_nil zone.parse('foobar')
+ assert_nil zone.parse(' ')
+ end
+ end
uses_mocha 'TestParseWithIncompleteDate' do
def test_parse_with_incomplete_date
Please sign in to comment.
Something went wrong with that request. Please try again.