From 06a7c2948a8dbf31357b552d468fcf42002736e7 Mon Sep 17 00:00:00 2001 From: gbuesing Date: Thu, 8 May 2008 21:30:17 -0500 Subject: Time.zone.parse: return nil for strings with no date information --- activerecord/lib/active_record/attribute_methods.rb | 2 +- activesupport/lib/active_support/values/time_zone.rb | 4 +++- activesupport/test/time_zone_test.rb | 8 ++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index fde98bf19f..2db27226f2 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -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 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 -- cgit v1.2.3