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

Changeset 8934

Show
Ignore:
Timestamp:
02/27/08 22:53:54 (4 months ago)
Author:
bitsweat
Message:

Adding Time#end_of_day, _quarter, _week, and _year. Closes #9312.

Files:

Legend:

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

    r8886 r8934  
    11*SVN* 
     2 
     3* Adding Time#end_of_day, _quarter, _week, and _year.  #9312 [BigTitus] 
    24 
    35* Adding TimeWithZone#between? [Geoff Buesing] 
  • trunk/activesupport/lib/active_support/core_ext/date/calculations.rb

    r8198 r8934  
    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) 
     
    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 
     
    173187        end 
    174188        alias :at_beginning_of_year :beginning_of_year 
     189 
     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 
    175195 
    176196        # Convenience method which returns a new Date/DateTime representing the time 1 day ago 
  • trunk/activesupport/lib/active_support/core_ext/time/calculations.rb

    r8885 r8934  
    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) 
     
    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 
     
    200213        end 
    201214        alias :at_beginning_of_year :beginning_of_year 
     215 
     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 
    202221 
    203222        # Convenience method which returns a new Time representing the time 1 day ago 
  • trunk/activesupport/test/core_ext/date_ext_test.rb

    r8731 r8934  
    5959    assert_equal Date.new(2005,10,1), Date.new(2005,12,31).beginning_of_quarter 
    6060    assert_equal Date.new(2005,4,1),  Date.new(2005,6,30).beginning_of_quarter 
     61  end 
     62 
     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 
    6184  end 
    6285 
  • trunk/activesupport/test/core_ext/time_ext_test.rb

    r8885 r8934  
    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 
     
    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 
     121 
     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 
    91126 
    92127  def test_beginning_of_year