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) |
|---|
-
activesupport/lib/active_support/core_ext/date/calculations.rb
old new 141 141 alias :monday :beginning_of_week 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) 146 154 days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} … … 167 175 end 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 172 186 self.acts_like?(:time) ? change(:month => 1, :day => 1, :hour => 0, :min => 0, :sec => 0) : change(:month => 1, :day => 1) 173 187 end 174 188 alias :at_beginning_of_year :beginning_of_year 175 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 195 176 196 # Convenience method which returns a new Date/DateTime representing the time 1 day ago 177 197 def yesterday 178 198 self - 1 -
activesupport/lib/active_support/core_ext/time/calculations.rb
old new 154 154 alias :monday :beginning_of_week 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) 159 166 days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} … … 194 201 end 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 199 212 change(:month => 1,:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0) 200 213 end 201 214 alias :at_beginning_of_year :beginning_of_year 202 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 221 203 222 # Convenience method which returns a new Time representing the time 1 day ago 204 223 def yesterday 205 224 self.ago(1.day) -
activesupport/test/core_ext/date_ext_test.rb
old new 60 60 assert_equal Date.new(2005,4,1), Date.new(2005,6,30).beginning_of_quarter 61 61 end 62 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 84 end 85 63 86 def test_end_of_month 64 87 assert_equal Date.new(2005,3,31), Date.new(2005,3,20).end_of_month 65 88 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 83 83 assert_equal Time.local(2005,4,1,0,0,0), Time.local(2005,6,30,23,59,59).beginning_of_quarter 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 88 111 assert_equal Time.local(2005,2,28,23,59,59), Time.local(2005,2,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 91 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 126 92 127 def test_beginning_of_year 93 128 assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_year 94 129 end