public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/ddollar/rails.git
Search Repo:
Time#since behaves correctly when passed a Duration. Closes #11527 
[kemiller]
gbuesing (author)
Sat Apr 12 17:35:47 -0700 2008
commit  9620372a6dc7eeebdb04f1fdb7f150d29e60fc00
tree    602897010f85ae36f4849bd6ba0c0c9010986191
parent  f285b6119b9ca7f598e31c0c8518dce3e1b13386
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Time#since behaves correctly when passed a Duration. Closes #11527 [kemiller]
0
+
0
 * Add #getutc alias for DateTime#utc [Geoff Buesing]
0
 
0
 * Refactor TimeWithZone: don't send #since, #ago, #+, #-, #advance through method_missing [Geoff Buesing]
...
88
89
90
91
92
93
94
95
96
97
 
98
99
100
101
102
 
 
 
 
 
 
 
103
104
105
...
88
89
90
 
91
92
93
94
95
 
96
97
 
98
 
 
99
100
101
102
103
104
105
106
107
108
0
@@ -88,18 +88,21 @@
0
         end
0
 
0
         # Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension
0
- # Do not use this method in combination with x.months, use months_ago instead!
0
         def ago(seconds)
0
           self.since(-seconds)
0
         end
0
 
0
         # Returns a new Time representing the time a number of seconds since the instance time, this is basically a wrapper around
0
- # the Numeric extension. Do not use this method in combination with x.months, use months_since instead!
0
+ # the Numeric extension.
0
         def since(seconds)
0
- initial_dst = self.dst? ? 1 : 0
0
           f = seconds.since(self)
0
- final_dst = f.dst? ? 1 : 0
0
- (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
0
+ if ActiveSupport::Duration === seconds
0
+ f
0
+ else
0
+ initial_dst = self.dst? ? 1 : 0
0
+ final_dst = f.dst? ? 1 : 0
0
+ (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
0
+ end
0
         rescue
0
           self.to_datetime.since(seconds)
0
         end
...
205
206
207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
209
210
...
227
228
229
 
 
 
 
 
 
 
 
 
 
 
 
 
230
231
232
...
240
241
242
243
 
244
245
246
...
266
267
268
 
 
 
 
 
 
 
 
 
 
 
 
 
269
270
271
...
279
280
281
282
 
283
284
285
...
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
...
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
...
278
279
280
 
281
282
283
284
...
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
...
330
331
332
 
333
334
335
336
0
@@ -205,6 +205,31 @@
0
     end
0
   end
0
 
0
+ def test_daylight_savings_time_crossings_backward_start_1day
0
+ with_env_tz 'US/Eastern' do
0
+ # dt: US: 2005 April 3rd 4:18am
0
+ assert_equal Time.local(2005,4,2,4,18,0), Time.local(2005,4,3,4,18,0).ago(1.day), 'dt-1.day=>st'
0
+ assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(1.day), 'st-1.day=>st'
0
+ end
0
+ with_env_tz 'NZ' do
0
+ # dt: New Zealand: 2006 October 1st 4:18am
0
+ assert_equal Time.local(2006,9,30,4,18,0), Time.local(2006,10,1,4,18,0).ago(1.day), 'dt-1.day=>st'
0
+ assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(1.day), 'st-1.day=>st'
0
+ end
0
+ end
0
+
0
+ def test_daylight_savings_time_crossings_backward_end_1day
0
+ with_env_tz 'US/Eastern' do
0
+ # st: US: 2005 October 30th 4:03am
0
+ assert_equal Time.local(2005,10,29,4,3), Time.local(2005,10,30,4,3,0).ago(1.day), 'st-1.day=>dt'
0
+ assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(1.day), 'dt-1.day=>dt'
0
+ end
0
+ with_env_tz 'NZ' do
0
+ # st: New Zealand: 2006 March 19th 4:03am
0
+ assert_equal Time.local(2006,3,18,4,3), Time.local(2006,3,19,4,3,0).ago(1.day), 'st-1.day=>dt'
0
+ assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(1.day), 'dt-1.day=>dt'
0
+ end
0
+ end
0
   def test_since
0
     assert_equal Time.local(2005,2,22,10,10,11), Time.local(2005,2,22,10,10,10).since(1)
0
     assert_equal Time.local(2005,2,22,11,10,10), Time.local(2005,2,22,10,10,10).since(3600)
0
@@ -227,6 +252,19 @@
0
     end
0
   end
0
 
0
+ def test_daylight_savings_time_crossings_forward_start_1day
0
+ with_env_tz 'US/Eastern' do
0
+ # st: US: 2005 April 2nd 7:27pm
0
+ assert_equal Time.local(2005,4,3,19,27,0), Time.local(2005,4,2,19,27,0).since(1.day), 'st+1.day=>dt'
0
+ assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(1.day), 'dt+1.day=>dt'
0
+ end
0
+ with_env_tz 'NZ' do
0
+ # st: New Zealand: 2006 September 30th 7:27pm
0
+ assert_equal Time.local(2006,10,1,19,27,0), Time.local(2006,9,30,19,27,0).since(1.day), 'st+1.day=>dt'
0
+ assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(1.day), 'dt+1.day=>dt'
0
+ end
0
+ end
0
+
0
   def test_daylight_savings_time_crossings_forward_start_tomorrow
0
     with_env_tz 'US/Eastern' do
0
       # st: US: 2005 April 2nd 7:27pm
0
@@ -240,7 +278,7 @@
0
     end
0
   end
0
 
0
- def test_daylight_savings_time_crossings_forward_start_yesterday
0
+ def test_daylight_savings_time_crossings_backward_start_yesterday
0
     with_env_tz 'US/Eastern' do
0
       # st: US: 2005 April 2nd 7:27pm
0
       assert_equal Time.local(2005,4,2,19,27,0), Time.local(2005,4,3,19,27,0).yesterday, 'dt-1.day=>st'
0
@@ -266,6 +304,19 @@
0
     end
0
   end
0
 
0
+ def test_daylight_savings_time_crossings_forward_end_1day
0
+ with_env_tz 'US/Eastern' do
0
+ # dt: US: 2005 October 30th 12:45am
0
+ assert_equal Time.local(2005,10,31,0,45,0), Time.local(2005,10,30,0,45,0).since(1.day), 'dt+1.day=>st'
0
+ assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(1.day), 'st+1.day=>st'
0
+ end
0
+ with_env_tz 'NZ' do
0
+ # dt: New Zealand: 2006 March 19th 1:45am
0
+ assert_equal Time.local(2006,3,20,1,45,0), Time.local(2006,3,19,1,45,0).since(1.day), 'dt+1.day=>st'
0
+ assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(1.day), 'st+1.day=>st'
0
+ end
0
+ end
0
+
0
   def test_daylight_savings_time_crossings_forward_end_tomorrow
0
     with_env_tz 'US/Eastern' do
0
       # dt: US: 2005 October 30th 12:45am
0
@@ -279,7 +330,7 @@
0
     end
0
   end
0
 
0
- def test_daylight_savings_time_crossings_forward_end_yesterday
0
+ def test_daylight_savings_time_crossings_backward_end_yesterday
0
     with_env_tz 'US/Eastern' do
0
       # dt: US: 2005 October 30th 12:45am
0
       assert_equal Time.local(2005,10,30,0,45,0), Time.local(2005,10,31,0,45,0).yesterday, 'st-1.day=>dt'

Comments

    No one has commented yet.