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

Changeset 5388

Show
Ignore:
Timestamp:
11/02/06 01:52:11 (2 years ago)
Author:
bitsweat
Message:

next_week respects DST changes. Closes #5617, closes #2353, closes #2509, references #4551.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r5386 r5388  
    33* Update dependencies to allow constants to be defined alongside their siblings. A common case for this is AR model classes with STI; user.rb might define User, Administrator and Guest for example. [Nicholas Seckar] 
    44 
    5 * next_week respects DST changes.  #6483 [marclove
     5* next_week respects DST changes.  #6483, #5617, #2353, #2509, #4551 [marclove, rabiedenharn, rails@roetzel.de, jsolson@damogran.org, drbrain@segment7.net
    66 
    77* Expose methods added to Enumerable in the documentation, such as group_by. Closes #6170. [sergeykojin@gmail.com, Marcel Molina Jr.] 
  • trunk/activesupport/lib/active_support/core_ext/time/calculations.rb

    r5363 r5388  
    2626        # Seconds since midnight: Time.now.seconds_since_midnight 
    2727        def seconds_since_midnight 
    28           self.hour.hours + self.min.minutes + self.sec + (self.usec/1.0e+6) 
     28          self.to_i - self.change(:hour => 0).to_i + (self.usec/1.0e+6) 
    2929        end 
    3030             
     
    5757        # Do not use this method in combination with x.months, use months_ago instead! 
    5858        def ago(seconds) 
    59           seconds.until(self
     59          self.since(-seconds
    6060        end 
    6161 
     
    6363        #the Numeric extension. Do not use this method in combination with x.months, use months_since instead! 
    6464        def since(seconds) 
    65           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 
    6669        end 
    6770        alias :in :since 
     
    136139        def next_week(day = :monday) 
    137140          days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} 
    138           # Adjust in case of switches to or from daylight savings time 
    139           week_from_today = self.since(1.week) + (self.since(1.week) <=> self).hour 
    140           week_from_today.beginning_of_week.since(days_into_week[day].day).change(:hour => 0) 
     141          since(1.week).beginning_of_week.since(days_into_week[day].day).change(:hour => 0) 
    141142        end 
    142143         
  • trunk/activesupport/test/core_ext/time_ext_test.rb

    r5363 r5388  
    88    assert_equal 86399,Time.local(2005,1,1,23,59,59).seconds_since_midnight 
    99    assert_equal 60.00001,Time.local(2005,1,1,0,1,0,10).seconds_since_midnight 
     10  end 
     11 
     12  def test_seconds_since_midnight_at_daylight_savings_time_start 
     13    # dt: US: 2005 April 3rd 2:00am ST => April 3rd 3:00am DT 
     14    assert_equal 3600+59*60+59,  Time.local(2005,4,3,1,59,59).seconds_since_midnight, 'just before DST start' 
     15    assert_equal 3600+59*60+59+2,Time.local(2005,4,3,3, 0, 1).seconds_since_midnight, 'just after DST start' 
     16  end 
     17 
     18  def test_seconds_since_midnight_at_daylight_savings_time_end 
     19    # st: US: 2005 October 30th 2:00am DT => October 30th 1:00am ST 
     20    # avoid setting a time between 1:00 and 2:00 since that requires specifying whether DST is active 
     21    assert_equal 3599, Time.local(2005,10,30,0,59,59).seconds_since_midnight, 'just before DST end' 
     22    assert_equal 3*3600+1, Time.local(2005,10,30,2, 0, 1).seconds_since_midnight, 'just after DST end' 
     23 
     24    # now set a time between 1:00 and 2:00 by specifying whether DST is active 
     25    # uses: Time.local( sec, min, hour, day, month, year, wday, yday, isdst, tz ) 
     26    assert_equal 1*3600+30*60, Time.local(0,30,1,30,10,2005,0,0,true,'EST5EDT').seconds_since_midnight, 'before DST end' 
     27    assert_equal 2*3600+30*60, Time.local(0,30,1,30,10,2005,0,0,false,'EST5EDT').seconds_since_midnight, 'after DST end' 
    1028  end 
    1129 
     
    89107  end 
    90108 
     109  def test_daylight_savings_time_crossings_backward_start 
     110    # dt: US: 2005 April 3rd 4:18am 
     111    assert_equal Time.local(2005,4,2,4,18,0), Time.local(2005,4,3,4,18,0).ago(86400), 'dt-1.day=>st' 
     112    assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(86400), 'st-1.day=>st' 
     113  end 
     114 
     115  def test_daylight_savings_time_crossings_backward_end 
     116    # st: US: 2005 October 30th 4:03am 
     117    assert_equal Time.local(2005,10,29,4,3), Time.local(2005,10,30,4,3,0).ago(86400), 'st-1.day=>dt' 
     118    assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(86400), 'dt-1.day=>dt' 
     119  end 
     120 
    91121  def test_since 
    92122    assert_equal Time.local(2005,2,22,10,10,11), Time.local(2005,2,22,10,10,10).since(1) 
     
    94124    assert_equal Time.local(2005,2,24,10,10,10), Time.local(2005,2,22,10,10,10).since(86400*2) 
    95125    assert_equal Time.local(2005,2,24,11,10,35), Time.local(2005,2,22,10,10,10).since(86400*2 + 3600 + 25) 
     126  end 
     127 
     128  def test_daylight_savings_time_crossings_forward_start 
     129    # st: US: 2005 April 2nd 7:27pm 
     130    assert_equal Time.local(2005,4,3,19,27,0), Time.local(2005,4,2,19,27,0).since(86400), 'st+1.day=>dt' 
     131    assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(86400), 'dt+1.day=>dt' 
     132  end 
     133 
     134  def test_daylight_savings_time_crossings_forward_end 
     135    # dt: US: 2005 October 30th 12:45am 
     136    assert_equal Time.local(2005,10,31,1,45,0), Time.local(2005,10,30,1,45,0).since(86400), 'dt+1.day=>st' 
     137    assert_equal Time.local(2005,11, 1,1,45,0), Time.local(2005,10,31,1,45,0).since(86400), 'st+1.day=>st' 
    96138  end 
    97139 
     
    149191    assert_equal Time.local(2006,10,30), Time.local(2006,10,23,0,0,0).next_week 
    150192    assert_equal Time.local(2006,11,1), Time.local(2006,10,23,0,0,0).next_week(:wednesday) 
     193  end 
     194 
     195  def test_next_week_near_daylight_start 
     196    assert_equal Time.local(2006,4,3), Time.local(2006,4,2,23,1,0).next_week, 'just crossed standard => daylight' 
     197  end 
     198 
     199  def test_next_week_near_daylight_end 
     200    assert_equal Time.local(2006,10,30), Time.local(2006,10,29,23,1,0).next_week, 'just crossed daylight => standard' 
    151201  end 
    152202