diff options
author | gbuesing <gbuesing@gmail.com> | 2008-05-08 21:30:17 -0500 |
---|---|---|
committer | gbuesing <gbuesing@gmail.com> | 2008-05-08 21:30:17 -0500 |
commit | 06a7c2948a8dbf31357b552d468fcf42002736e7 (patch) | |
tree | 94a646f54e820dff7dbad3bc0a3928b1c5731fe6 /activesupport | |
parent | fb9bf16e96e2c24d4996327500d8109b5d535e2c (diff) | |
download | rails-06a7c2948a8dbf31357b552d468fcf42002736e7.tar.gz rails-06a7c2948a8dbf31357b552d468fcf42002736e7.tar.bz2 rails-06a7c2948a8dbf31357b552d468fcf42002736e7.zip |
Time.zone.parse: return nil for strings with no date information
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/values/time_zone.rb | 4 | ||||
-rw-r--r-- | activesupport/test/time_zone_test.rb | 8 |
2 files changed, 11 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index d597d99880..0fa99135e2 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -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) diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb index 0bfb89b675..3757ddcedb 100644 --- a/activesupport/test/time_zone_test.rb +++ b/activesupport/test/time_zone_test.rb @@ -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 |