Changeset 8934
- Timestamp:
- 02/27/08 22:53:54 (4 months ago)
- Files:
-
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/date/calculations.rb (modified) (3 diffs)
- trunk/activesupport/lib/active_support/core_ext/time/calculations.rb (modified) (3 diffs)
- trunk/activesupport/test/core_ext/date_ext_test.rb (modified) (1 diff)
- trunk/activesupport/test/core_ext/time_ext_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/CHANGELOG
r8886 r8934 1 1 *SVN* 2 3 * Adding Time#end_of_day, _quarter, _week, and _year. #9312 [BigTitus] 2 4 3 5 * Adding TimeWithZone#between? [Geoff Buesing] trunk/activesupport/lib/active_support/core_ext/date/calculations.rb
r8198 r8934 142 142 alias :at_beginning_of_week :beginning_of_week 143 143 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 144 152 # Returns a new Date/DateTime representing the start of the given day in next week (default is Monday). 145 153 def next_week(day = :monday) … … 168 176 alias :at_beginning_of_quarter :beginning_of_quarter 169 177 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 170 184 # Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00) 171 185 def beginning_of_year … … 173 187 end 174 188 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 175 195 176 196 # 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 155 155 alias :at_beginning_of_week :beginning_of_week 156 156 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 157 164 # Returns a new Time representing the start of the given day in next week (default is Monday). 158 165 def next_week(day = :monday) … … 195 202 alias :at_beginning_of_quarter :beginning_of_quarter 196 203 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 197 210 # Returns a new Time representing the start of the year (1st of january, 0:00) 198 211 def beginning_of_year … … 200 213 end 201 214 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 202 221 203 222 # Convenience method which returns a new Time representing the time 1 day ago trunk/activesupport/test/core_ext/date_ext_test.rb
r8731 r8934 59 59 assert_equal Date.new(2005,10,1), Date.new(2005,12,31).beginning_of_quarter 60 60 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 61 84 end 62 85 trunk/activesupport/test/core_ext/time_ext_test.rb
r8885 r8934 84 84 end 85 85 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 86 109 def test_end_of_month 87 110 assert_equal Time.local(2005,3,31,23,59,59), Time.local(2005,3,20,10,10,10).end_of_month … … 89 112 assert_equal Time.local(2005,4,30,23,59,59), Time.local(2005,4,20,10,10,10).end_of_month 90 113 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 91 126 92 127 def test_beginning_of_year