public
Fork of NZKoz/koz-rails
Description: Koz's rails git-svn clone
Homepage: http://www.rubyonrails.org
Clone URL: git://github.com/eventualbuddha/koz-rails.git
TimeWithZone instances correctly enforce DST rules. Adding 
TimeZone#period_for_utc

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9006 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
gbuesing (author)
Mon Mar 10 21:26:20 -0700 2008
commit  5fcffd53c2a54f9e0db55d244abb91ce037ee240
tree    90e89994fc4486e5523fe45df46f5d013e184a83
parent  323c95914de735a2a487d59c01e48dc635b1308d
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* TimeWithZone instances correctly enforce DST rules. Adding TimeZone#period_for_utc [Geoff Buesing]
0
+
0
 * test_time_with_datetime_fallback expects DateTime.local_offset instead of DateTime.now.offset [Geoff Buesing]
0
 
0
 * Adding TimeWithZone #marshal_dump and #marshal_load [Geoff Buesing]
...
27
28
29
30
 
31
32
33
...
150
151
152
 
 
 
 
 
 
 
 
 
153
154
155
...
206
207
208
209
210
 
 
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
...
27
28
29
 
30
31
32
33
...
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
...
215
216
217
 
 
218
219
220
221
 
 
 
 
 
 
 
 
 
 
 
222
223
0
@@ -27,7 +27,7 @@ module ActiveSupport
0
   
0
     # Returns the underlying TZInfo::TimezonePeriod for the local time
0
     def period
0
- @period ||= get_period_for_local
0
+ @period ||= time_zone.period_for_utc(utc)
0
     end
0
 
0
     # Returns the simultaneous time in the specified zone
0
@@ -150,6 +150,15 @@ module ActiveSupport
0
       end
0
     end
0
     
0
+ %w(asctime day hour min mon sec usec wday yday year).each do |name|
0
+ define_method(name) do
0
+ time.__send__(name)
0
+ end
0
+ end
0
+ alias_method :ctime, :asctime
0
+ alias_method :mday, :day
0
+ alias_method :month, :mon
0
+
0
     def to_a
0
       time.to_a[0, 8].push(dst?, zone)
0
     end
0
@@ -206,20 +215,9 @@ module ActiveSupport
0
   
0
     # Send the missing method to time instance, and wrap result in a new TimeWithZone with the existing time_zone
0
     def method_missing(sym, *args, &block)
0
- result = time.__send__(sym, *args, &block)
0
- result = result.change_time_zone(time_zone) if result.acts_like?(:time)
0
+ result = utc.__send__(sym, *args, &block)
0
+ result = result.in_time_zone(time_zone) if result.acts_like?(:time)
0
       result
0
     end
0
-
0
- private
0
- def get_period_for_local
0
- t = time
0
- begin
0
- time_zone.period_for_local(t, true)
0
- rescue ::TZInfo::PeriodNotFound # failover logic from TzTime
0
- t -= 1.hour
0
- retry
0
- end
0
- end
0
   end
0
 end
...
212
213
214
 
 
 
 
 
215
216
217
...
212
213
214
215
216
217
218
219
220
221
222
0
@@ -212,6 +212,11 @@ class TimeZone
0
     end
0
 
0
     # Available so that TimeZone instances respond like TZInfo::Timezone instances
0
+ def period_for_utc(time)
0
+ tzinfo.period_for_utc(time)
0
+ end
0
+
0
+ # Available so that TimeZone instances respond like TZInfo::Timezone instances
0
     def period_for_local(time, dst=true)
0
       tzinfo.period_for_local(time, dst)
0
     end
...
165
166
167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
169
170
...
219
220
221
222
223
224
 
 
 
 
 
 
 
 
225
226
227
...
165
166
167
168
169
170
171
172
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
...
251
252
253
 
 
 
254
255
256
257
258
259
260
261
262
263
264
0
@@ -165,6 +165,38 @@ uses_tzinfo 'TimeWithZoneTest' do
0
       assert_equal 86_400.0, twz2 - twz1
0
     end
0
     
0
+ def test_plus_and_minus_enforce_spring_dst_rules
0
+ utc = Time.utc(2006,4,2,6,59,59) # == Apr 2 2006 01:59:59 EST; i.e., 1 second before daylight savings start
0
+ twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
0
+ assert_equal Time.utc(2006,4,2,1,59,59), twz.time
0
+ assert_equal false, twz.dst?
0
+ assert_equal 'EST', twz.zone
0
+ twz = twz + 1
0
+ assert_equal Time.utc(2006,4,2,3), twz.time # adding 1 sec springs forward to 3:00AM EDT
0
+ assert_equal true, twz.dst?
0
+ assert_equal 'EDT', twz.zone
0
+ twz = twz - 1 # subtracting 1 second takes goes back to 1:59:59AM EST
0
+ assert_equal Time.utc(2006,4,2,1,59,59), twz.time
0
+ assert_equal false, twz.dst?
0
+ assert_equal 'EST', twz.zone
0
+ end
0
+
0
+ def test_plus_and_minus_enforce_fall_dst_rules
0
+ utc = Time.utc(2006,10,29,5,59,59) # == Oct 29 2006 01:59:59 EST; i.e., 1 second before daylight savings end
0
+ twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
0
+ assert_equal Time.utc(2006,10,29,1,59,59), twz.time
0
+ assert_equal true, twz.dst?
0
+ assert_equal 'EDT', twz.zone
0
+ twz = twz + 1
0
+ assert_equal Time.utc(2006,10,29,1), twz.time # adding 1 sec falls back from 1:59:59 EDT to 1:00AM EST
0
+ assert_equal false, twz.dst?
0
+ assert_equal 'EST', twz.zone
0
+ twz = twz - 1
0
+ assert_equal Time.utc(2006,10,29,1,59,59), twz.time # subtracting 1 sec goes back to 1:59:59AM EDT
0
+ assert_equal true, twz.dst?
0
+ assert_equal 'EDT', twz.zone
0
+ end
0
+
0
     def test_to_a
0
       assert_equal [45, 30, 5, 1, 2, 2000, 2, 32, false, "HST"], ActiveSupport::TimeWithZone.new( Time.utc(2000, 2, 1, 15, 30, 45), TimeZone['Hawaii'] ).to_a
0
     end
0
@@ -219,9 +251,14 @@ uses_tzinfo 'TimeWithZoneTest' do
0
     end
0
       
0
     def test_method_missing_with_non_time_return_value
0
- assert_equal 1999, @twz.year
0
- assert_equal 12, @twz.month
0
- assert_equal 31, @twz.day
0
+ twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone)
0
+ assert_equal 1999, twz.year
0
+ assert_equal 12, twz.month
0
+ assert_equal 31, twz.day
0
+ assert_equal 14, twz.hour
0
+ assert_equal 18, twz.min
0
+ assert_equal 17, twz.sec
0
+ assert_equal 500, twz.usec
0
     end
0
   end
0
   

Comments

    No one has commented yet.