Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5617: handle_dst_changes_for_large_deltas.txt

File handle_dst_changes_for_large_deltas.txt, 3.5 kB (added by rob@AgileConsultingLLC.com, 2 years ago)
Line 
1 Index: activesupport/test/core_ext/time_ext_test.rb
2 ===================================================================
3 --- activesupport/test/core_ext/time_ext_test.rb        (revision 4560)
4 +++ activesupport/test/core_ext/time_ext_test.rb        (working copy)
5 @@ -22,6 +22,30 @@
6      assert_equal Time.local(2005,11,28), Time.local(2005,12,04,0,0,0).beginning_of_week #sunday   
7    end
8    
9 +  def test_daylight_savings_time_crossings_forward_start
10 +    # st: April 2nd 7:27pm
11 +    assert_equal Time.local(2005,4,3,19,27,0), Time.local(2005,4,2,19,27,0).since(86400), 'st+1.day=>dt'
12 +    assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(86400), 'dt+1.day=>dt'
13 +  end
14
15 +  def test_daylight_savings_time_crossings_forward_end
16 +    # dt: October 30th 12:45am
17 +    assert_equal Time.local(2005,10,31,1,45,0), Time.local(2005,10,30,1,45,0).since(86400), 'dt+1.day=>st'
18 +    assert_equal Time.local(2005,11, 1,1,45,0), Time.local(2005,10,31,1,45,0).since(86400), 'st+1.day=>st'
19 +  end
20
21 +  def test_daylight_savings_time_crossings_backward_start
22 +    # dt: April 3rd 4:18am
23 +    assert_equal Time.local(2005,4,2,4,18,0), Time.local(2005,4,3,4,18,0).ago(86400), 'dt-1.day=>st'
24 +    assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(86400), 'st-1.day=>st'
25 +  end
26
27 +  def test_daylight_savings_time_crossings_backward_end
28 +    # st: October 30th 4:03am
29 +    assert_equal Time.local(2005,10,29,4,3), Time.local(2005,10,30,4,3,0).ago(86400), 'st-1.day=>dt'
30 +    assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(86400), 'dt-1.day=>dt'
31 +  end
32 +
33    def test_beginning_of_day
34      assert_equal Time.local(2005,2,4,0,0,0), Time.local(2005,2,4,10,10,10).beginning_of_day
35    end
36 Index: activesupport/lib/active_support/core_ext/time/calculations.rb
37 ===================================================================
38 --- activesupport/lib/active_support/core_ext/time/calculations.rb      (revision 4560)
39 +++ activesupport/lib/active_support/core_ext/time/calculations.rb      (working copy)
40 @@ -25,7 +25,11 @@
41  
42          # Seconds since midnight: Time.now.seconds_since_midnight
43          def seconds_since_midnight
44 -          self.hour.hours + self.min.minutes + self.sec + (self.usec/1.0e+6)
45 +          initial_dst = self.dst? ? 1 : 0
46 +          final_dst   = self.change(:hour => 0).dst? ? 1 : 0
47 +
48 +          dst_adj =  (initial_dst != final_dst) ? (final_dst - initial_dst).hours : 0
49 +          self.hour.hours + self.min.minutes + self.sec + (self.usec/1.0e+6) - dst_adj
50          end
51              
52          # Returns a new Time where one or more of the elements have been changed according to the +options+ parameter. The time options
53 @@ -56,13 +60,16 @@
54          # Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension
55          # Do not use this method in combination with x.months, use months_ago instead!
56          def ago(seconds)
57 -          seconds.until(self)
58 +          self.since(-seconds)
59          end
60  
61          # Returns a new Time representing the time a number of seconds since the instance time, this is basically a wrapper around
62          #the Numeric extension. Do not use this method in combination with x.months, use months_since instead!
63          def since(seconds)
64 -          seconds.since(self)
65 +          initial_dst = self.dst? ? 1 : 0
66 +          f = seconds.since(self)
67 +          final_dst   = f.dst? ? 1 : 0
68 +          (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
69          end
70          alias :in :since
71