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

Ticket #9312: updated_time_end_of_methods.diff

File updated_time_end_of_methods.diff, 8.9 kB (added by juanjo.bazan, 11 months ago)

end_of_XX methods updated with Geoff suggestions

  • activesupport/lib/active_support/core_ext/date/calculations.rb

    old new  
    141141        alias :monday :beginning_of_week 
    142142        alias :at_beginning_of_week :beginning_of_week 
    143143 
     144        # Returns a new Date/DateTime representing the end of this week (Sunday, DateTime objects will have time set to 23:59:59) 
     145        def end_of_week 
     146          days_to_sunday = self.wday!=0 ? 7-self.wday : 0 
     147          result = self + days_to_sunday.days 
     148          self.acts_like?(:time) ? result.end_of_day : result 
     149        end         
     150        alias :at_end_of_week :end_of_week 
     151 
    144152        # Returns a new Date/DateTime representing the start of the given day in next week (default is Monday). 
    145153        def next_week(day = :monday) 
    146154          days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} 
     
    167175        end 
    168176        alias :at_beginning_of_quarter :beginning_of_quarter 
    169177 
     178        # Returns a new Date/DateTime representing the end of the quarter (last day of march, june, september, december; DateTime objects will have time set to 23:59:59) 
     179        def end_of_quarter 
     180          change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month 
     181        end 
     182        alias :at_end_of_quarter :end_of_quarter 
     183 
    170184        # Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00) 
    171185        def beginning_of_year 
    172186          self.acts_like?(:time) ? change(:month => 1, :day => 1, :hour => 0, :min => 0, :sec => 0) : change(:month => 1, :day => 1) 
    173187        end 
    174188        alias :at_beginning_of_year :beginning_of_year 
    175189 
     190        # Returns a new Time representing the end of the year (31st of december; DateTime objects will have time set to 23:59:59) 
     191        def end_of_year 
     192          self.acts_like?(:time) ? change(:month => 12,:day => 31,:hour => 23, :min => 59, :sec => 59) : change(:month => 12, :day => 31) 
     193        end 
     194        alias :at_end_of_year :end_of_year 
     195 
    176196        # Convenience method which returns a new Date/DateTime representing the time 1 day ago 
    177197        def yesterday 
    178198          self - 1 
  • activesupport/lib/active_support/core_ext/time/calculations.rb

    old new  
    154154        alias :monday :beginning_of_week 
    155155        alias :at_beginning_of_week :beginning_of_week 
    156156 
     157        # Returns a new Time representing the end of this week (Sunday, 23:59:59) 
     158        def end_of_week 
     159          days_to_sunday = self.wday!=0 ? 7-self.wday : 0 
     160          (self + days_to_sunday.days).end_of_day 
     161        end         
     162        alias :at_end_of_week :end_of_week 
     163 
    157164        # Returns a new Time representing the start of the given day in next week (default is Monday). 
    158165        def next_week(day = :monday) 
    159166          days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} 
     
    194201        end 
    195202        alias :at_beginning_of_quarter :beginning_of_quarter 
    196203 
     204        # Returns a new Time representing the end of the quarter (last day of march, june, september, december, 23:59:59) 
     205        def end_of_quarter 
     206          change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month 
     207        end 
     208        alias :at_end_of_quarter :end_of_quarter 
     209 
    197210        # Returns  a new Time representing the start of the year (1st of january, 0:00) 
    198211        def beginning_of_year 
    199212          change(:month => 1,:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0) 
    200213        end 
    201214        alias :at_beginning_of_year :beginning_of_year 
    202215 
     216        # Returns a new Time representing the end of the year (31st of december, 23:59:59) 
     217        def end_of_year 
     218          change(:month => 12,:day => 31,:hour => 23, :min => 59, :sec => 59) 
     219        end 
     220        alias :at_end_of_year :end_of_year 
     221 
    203222        # Convenience method which returns a new Time representing the time 1 day ago 
    204223        def yesterday 
    205224          self.ago(1.day) 
  • activesupport/test/core_ext/date_ext_test.rb

    old new  
    6060    assert_equal Date.new(2005,4,1),  Date.new(2005,6,30).beginning_of_quarter 
    6161  end 
    6262 
     63  def test_end_of_week 
     64    assert_equal Date.new(2008,2,24), Date.new(2008,2,22).end_of_week 
     65    assert_equal Date.new(2008,3,2), Date.new(2008,2,25).end_of_week #monday 
     66    assert_equal Date.new(2008,3,2), Date.new(2008,2,26).end_of_week #tuesday 
     67    assert_equal Date.new(2008,3,2), Date.new(2008,2,27).end_of_week #wednesday 
     68    assert_equal Date.new(2008,3,2), Date.new(2008,2,28).end_of_week #thursday 
     69    assert_equal Date.new(2008,3,2), Date.new(2008,2,29).end_of_week #friday 
     70    assert_equal Date.new(2008,3,2), Date.new(2008,3,01).end_of_week #saturday 
     71    assert_equal Date.new(2008,3,2), Date.new(2008,3,02).end_of_week #sunday 
     72  end 
     73 
     74  def test_end_of_quarter 
     75    assert_equal Date.new(2008,3,31),  Date.new(2008,2,15).end_of_quarter 
     76    assert_equal Date.new(2008,3,31),  Date.new(2008,3,31).end_of_quarter 
     77    assert_equal Date.new(2008,12,31), Date.new(2008,10,8).end_of_quarter 
     78    assert_equal Date.new(2008,6,30),  Date.new(2008,4,14).end_of_quarter 
     79    assert_equal Date.new(2008,9,30),  Date.new(2008,8,21).end_of_quarter 
     80  end 
     81 
     82  def test_end_of_year 
     83    assert_equal Date.new(2008,12,31).to_s, Date.new(2008,2,22).end_of_year.to_s 
     84  end 
     85 
    6386  def test_end_of_month 
    6487    assert_equal Date.new(2005,3,31), Date.new(2005,3,20).end_of_month 
    6588    assert_equal Date.new(2005,2,28), Date.new(2005,2,20).end_of_month 
  • activesupport/test/core_ext/time_ext_test.rb

    old new  
    8383    assert_equal Time.local(2005,4,1,0,0,0), Time.local(2005,6,30,23,59,59).beginning_of_quarter 
    8484  end 
    8585 
     86  def test_end_of_day 
     87    assert_equal Time.local(2007,8,12,23,59,59), Time.local(2007,8,12,10,10,10).end_of_day 
     88    with_env_tz 'US/Eastern' do 
     89      assert_equal Time.local(2007,4,2,23,59,59), Time.local(2007,4,2,10,10,10).end_of_day, 'start DST' 
     90      assert_equal Time.local(2007,10,29,23,59,59), Time.local(2007,10,29,10,10,10).end_of_day, 'ends DST' 
     91    end 
     92    with_env_tz 'NZ' do 
     93      assert_equal Time.local(2006,3,19,23,59,59), Time.local(2006,3,19,10,10,10).end_of_day, 'ends DST' 
     94      assert_equal Time.local(2006,10,1,23,59,59), Time.local(2006,10,1,10,10,10).end_of_day, 'start DST' 
     95    end 
     96  end 
     97   
     98  def test_end_of_week 
     99    assert_equal Time.local(2008,1,6,23,59,59), Time.local(2007,12,31,10,10,10).end_of_week 
     100    assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,8,27,0,0,0).end_of_week #monday 
     101    assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,8,28,0,0,0).end_of_week #tuesday 
     102    assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,8,29,0,0,0).end_of_week #wednesday 
     103    assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,8,30,0,0,0).end_of_week #thursday 
     104    assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,8,31,0,0,0).end_of_week #friday 
     105    assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,9,01,0,0,0).end_of_week #saturday 
     106    assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,9,02,0,0,0).end_of_week #sunday 
     107  end 
     108 
    86109  def test_end_of_month 
    87110    assert_equal Time.local(2005,3,31,23,59,59), Time.local(2005,3,20,10,10,10).end_of_month 
    88111    assert_equal Time.local(2005,2,28,23,59,59), Time.local(2005,2,20,10,10,10).end_of_month 
    89112    assert_equal Time.local(2005,4,30,23,59,59), Time.local(2005,4,20,10,10,10).end_of_month 
    90113  end 
     114   
     115  def test_end_of_quarter 
     116    assert_equal Time.local(2007,3,31,23,59,59), Time.local(2007,2,15,10,10,10).end_of_quarter 
     117    assert_equal Time.local(2007,3,31,23,59,59), Time.local(2007,3,31,0,0,0).end_of_quarter 
     118    assert_equal Time.local(2007,12,31,23,59,59), Time.local(2007,12,21,10,10,10).end_of_quarter 
     119    assert_equal Time.local(2007,6,30,23,59,59), Time.local(2007,4,1,0,0,0).end_of_quarter 
     120  end 
    91121 
     122  def test_end_of_year 
     123    assert_equal Time.local(2007,12,31,23,59,59), Time.local(2007,2,22,10,10,10).end_of_year 
     124    assert_equal Time.local(2007,12,31,23,59,59), Time.local(2007,12,31,10,10,10).end_of_year 
     125   end 
     126 
    92127  def test_beginning_of_year 
    93128    assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_year 
    94129  end