public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
Time.zone.parse: return nil for strings with no date information
gbuesing (author)
Thu May 08 19:30:17 -0700 2008
commit  06a7c2948a8dbf31357b552d468fcf42002736e7
tree    94a646f54e820dff7dbad3bc0a3928b1c5731fe6
parent  fb9bf16e96e2c24d4996327500d8109b5d535e2c
...
179
180
181
182
 
183
184
185
...
179
180
181
 
182
183
184
185
0
@@ -179,7 +179,7 @@
0
         def define_write_method_for_time_zone_conversion(attr_name)
0
           method_body = <<-EOV
0
             def #{attr_name}=(time)
0
- unless time.blank? || time.acts_like?(:time)
0
+ unless time. time.acts_like?(:time)
0
                 time = time.is_a?(String) ? Time.zone.parse(time) : time.to_time rescue time
0
               end
0
               time = time.in_time_zone rescue nil if time
...
213
214
215
 
 
216
217
 
218
219
220
...
213
214
215
216
217
218
 
219
220
221
222
0
@@ -213,8 +213,10 @@
0
   # Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
0
   # Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
0
   def parse(str, now=now)
0
+ date_parts = Date._parse(str)
0
+ return if date_parts.blank?
0
     time = Time.parse(str, now) rescue DateTime.parse(str)
0
- if Date._parse(str)[:offset].nil?
0
+ if date_parts[:offset].nil?
0
       ActiveSupport::TimeWithZone.new(nil, self, time)
0
     else
0
       time.in_time_zone(self)
...
198
199
200
 
 
 
 
 
 
 
 
201
202
203
...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
0
@@ -198,6 +198,14 @@
0
       assert_equal zone, twz.time_zone
0
     end
0
   end
0
+
0
+ def test_parse_returns_nil_when_string_without_date_information_is_passed_in
0
+ silence_warnings do # silence warnings raised by tzinfo gem
0
+ zone = TimeZone['Eastern Time (US & Canada)']
0
+ assert_nil zone.parse('foobar')
0
+ assert_nil zone.parse(' ')
0
+ end
0
+ end
0
 
0
   uses_mocha 'TestParseWithIncompleteDate' do
0
     def test_parse_with_incomplete_date

Comments

    No one has commented yet.