提交 06a7c294 编写于 作者: G gbuesing

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

上级 fb9bf16e
......@@ -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
......
......@@ -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)
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册