Skip to content
This repository
Browse code

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

  • Loading branch information...
commit 06a7c2948a8dbf31357b552d468fcf42002736e7 1 parent fb9bf16
Geoff Buesing authored May 08, 2008
2  activerecord/lib/active_record/attribute_methods.rb
@@ -179,7 +179,7 @@ def define_write_method(attr_name)
179 179
         def define_write_method_for_time_zone_conversion(attr_name)
180 180
           method_body = <<-EOV
181 181
             def #{attr_name}=(time)
182  
-              unless time.blank? || time.acts_like?(:time)
  182
+              unless time.acts_like?(:time)
183 183
                 time = time.is_a?(String) ? Time.zone.parse(time) : time.to_time rescue time
184 184
               end
185 185
               time = time.in_time_zone rescue nil if time
4  activesupport/lib/active_support/values/time_zone.rb
@@ -213,8 +213,10 @@ def at(secs)
213 213
   #   Time.zone.now                 # => Fri, 31 Dec 1999 14:00:00 HST -10:00
214 214
   #   Time.zone.parse('22:30:00')   # => Fri, 31 Dec 1999 22:30:00 HST -10:00
215 215
   def parse(str, now=now)
  216
+    date_parts = Date._parse(str)
  217
+    return if date_parts.blank?
216 218
     time = Time.parse(str, now) rescue DateTime.parse(str)
217  
-    if Date._parse(str)[:offset].nil?
  219
+    if date_parts[:offset].nil?
218 220
       ActiveSupport::TimeWithZone.new(nil, self, time)
219 221
     else
220 222
       time.in_time_zone(self)
8  activesupport/test/time_zone_test.rb
@@ -198,6 +198,14 @@ def test_parse_far_future_date_with_time_zone_offset_in_string
198 198
       assert_equal zone, twz.time_zone
199 199
     end
200 200
   end
  201
+  
  202
+  def test_parse_returns_nil_when_string_without_date_information_is_passed_in
  203
+    silence_warnings do # silence warnings raised by tzinfo gem
  204
+      zone = TimeZone['Eastern Time (US & Canada)']
  205
+      assert_nil zone.parse('foobar')
  206
+      assert_nil zone.parse('   ')
  207
+    end
  208
+  end
201 209
 
202 210
   uses_mocha 'TestParseWithIncompleteDate' do
203 211
     def test_parse_with_incomplete_date

0 notes on commit 06a7c29

Please sign in to comment.
Something went wrong with that request. Please try again.