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

This commit is contained in:
gbuesing 2008-05-08 21:30:17 -05:00
parent fb9bf16e96
commit 06a7c2948a
3 changed files with 12 additions and 2 deletions

View File

@ -179,7 +179,7 @@ module ActiveRecord
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

View File

@ -213,8 +213,10 @@ class TimeZone
# 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)

View File

@ -198,6 +198,14 @@ class TimeZoneTest < Test::Unit::TestCase
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