public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Fix Time.zone.parse from stripping time zone information and make Time 
aware attribute methods use Time.zone.parse instead of to_time
nullstyle (author)
Wed May 07 06:54:07 -0700 2008
gbuesing (committer)
Thu May 08 17:25:31 -0700 2008
commit  eb5b93be74ed3eca925c1ab9bd4739919ae39a41
tree    47e40b6cdb3a13911d2c3ff75761b4e7c1e9775a
parent  bcb090c56b842a76397e0ea32f54c942fd11910e
...
180
181
182
183
 
184
185
186
...
180
181
182
 
183
184
185
186
0
@@ -180,7 +180,7 @@ module ActiveRecord
0
           method_body = <<-EOV
0
             def #{attr_name}=(time)
0
               if time
0
- time = time.to_time rescue time unless time.acts_like?(:time)
0
+ time = Time.zone.parse(time) rescue time unless time.acts_like?(:time)
0
                 time = time.in_time_zone if time.acts_like?(:time)
0
               end
0
               write_attribute(:#{attr_name}, time)
...
173
174
175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
177
178
...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
0
@@ -173,6 +173,33 @@ class AttributeMethodsTest < ActiveRecord::TestCase
0
     end
0
   end
0
 
0
+ def test_setting_time_zone_aware_attribute_with_string
0
+ utc_time = Time.utc(2008, 1, 1)
0
+ (-11..13).each do |timezone_offset|
0
+ time_string = utc_time.in_time_zone(timezone_offset).to_s
0
+ in_time_zone "Pacific Time (US & Canada)" do
0
+ record = @target.new
0
+ record.written_on = time_string
0
+ assert_equal Time.zone.parse(time_string), record.written_on
0
+ assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone
0
+ assert_equal Time.utc(2007, 12, 31, 16), record.written_on.time
0
+ end
0
+ end
0
+ end
0
+
0
+ def test_setting_time_zone_aware_attribute_interprets_time_zone_unaware_string_in_time_zone
0
+ time_string = 'Tue Jan 01 00:00:00 2008'
0
+ (-11..13).each do |timezone_offset|
0
+ in_time_zone timezone_offset do
0
+ record = @target.new
0
+ record.written_on = time_string
0
+ assert_equal Time.zone.parse(time_string), record.written_on
0
+ assert_equal TimeZone[timezone_offset], record.written_on.time_zone
0
+ assert_equal Time.utc(2008, 1, 1), record.written_on.time
0
+ end
0
+ end
0
+ end
0
+
0
   def test_setting_time_zone_aware_attribute_in_current_time_zone
0
     utc_time = Time.utc(2008, 1, 1)
0
     in_time_zone "Pacific Time (US & Canada)" do
...
214
215
216
 
 
 
217
218
219
...
214
215
216
217
218
219
220
221
222
0
@@ -214,6 +214,9 @@ class TimeZone
0
   # Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
0
   def parse(str, now=now)
0
     time = Time.parse(str, now) rescue DateTime.parse(str)
0
+ unless time.is_a?(DateTime) || Date._parse(str)[:offset].nil?
0
+ time += time.in_time_zone(self).utc_offset - time.utc_offset
0
+ end
0
     ActiveSupport::TimeWithZone.new(nil, self, time)
0
   end
0
 
...
173
174
175
 
 
 
 
 
 
 
 
176
177
178
...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
0
@@ -173,6 +173,14 @@ class TimeZoneTest < Test::Unit::TestCase
0
     assert_equal zone, twz.time_zone
0
   end
0
 
0
+ def test_parse_string_with_timezone
0
+ (-11..13).each do |timezone_offset|
0
+ zone = TimeZone[timezone_offset]
0
+ twz = zone.parse('1999-12-31 19:00:00')
0
+ assert_equal twz, zone.parse(twz.to_s)
0
+ end
0
+ end
0
+
0
   def test_parse_with_old_date
0
     silence_warnings do # silence warnings raised by tzinfo gem
0
       zone = TimeZone['Eastern Time (US & Canada)']

Comments

    No one has commented yet.