Skip to content

Commit 699ba8a

Browse files
committed
Merge pull request rails#4284 from mattdbridges/time_calculation_aliases
Added aliases for prev_year, prev_month, and prev_week in Time and Date calculations
2 parents 43faccf + d636662 commit 699ba8a

File tree

6 files changed

+80
-6
lines changed

6 files changed

+80
-6
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ def prev_week(day = :monday)
182182
result = (self - 7).beginning_of_week + DAYS_INTO_WEEK[day]
183183
self.acts_like?(:time) ? result.change(:hour => 0) : result
184184
end
185+
alias :last_week :prev_week
186+
187+
# Alias of prev_month
188+
alias :last_month :prev_month
189+
190+
# Alias of prev_year
191+
alias :last_year :prev_year
185192

186193
# Returns a new Date/DateTime representing the start of the given day in next week (default is :monday).
187194
def next_week(day = :monday)

activesupport/lib/active_support/core_ext/time/calculations.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ def years_since(years)
145145
def prev_year
146146
years_ago(1)
147147
end
148+
alias_method :last_year, :prev_year
148149

149150
# Short-hand for years_since(1)
150151
def next_year
@@ -155,6 +156,7 @@ def next_year
155156
def prev_month
156157
months_ago(1)
157158
end
159+
alias_method :last_month, :prev_month
158160

159161
# Short-hand for months_since(1)
160162
def next_month
@@ -199,6 +201,7 @@ def sunday
199201
def prev_week(day = :monday)
200202
ago(1.week).beginning_of_week.since(DAYS_INTO_WEEK[day].day).change(:hour => 0)
201203
end
204+
alias_method :last_week, :prev_week
202205

203206
# Returns a new Time representing the start of the given day in next week (default is :monday).
204207
def next_week(day = :monday)

activesupport/test/core_ext/date_ext_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ def test_prev_year_in_calendar_reform
175175
assert_equal Date.new(1582,10,4), Date.new(1583,10,14).prev_year
176176
end
177177

178+
def test_last_year
179+
assert_equal Date.new(2004,6,5), Date.new(2005,6,5).last_year
180+
end
181+
182+
def test_last_year_in_leap_years
183+
assert_equal Date.new(1999,2,28), Date.new(2000,2,29).last_year
184+
end
185+
186+
def test_last_year_in_calendar_reform
187+
assert_equal Date.new(1582,10,4), Date.new(1583,10,14).last_year
188+
end
189+
178190
def test_next_year
179191
assert_equal Date.new(2006,6,5), Date.new(2005,6,5).next_year
180192
end
@@ -245,6 +257,14 @@ def test_prev_week
245257
assert_equal Date.new(2010,2,27), Date.new(2010,3,4).prev_week(:saturday)
246258
end
247259

260+
def test_last_week
261+
assert_equal Date.new(2005,5,9), Date.new(2005,5,17).last_week
262+
assert_equal Date.new(2006,12,25), Date.new(2007,1,7).last_week
263+
assert_equal Date.new(2010,2,12), Date.new(2010,2,19).last_week(:friday)
264+
assert_equal Date.new(2010,2,13), Date.new(2010,2,19).last_week(:saturday)
265+
assert_equal Date.new(2010,2,27), Date.new(2010,3,4).last_week(:saturday)
266+
end
267+
248268
def test_next_week
249269
assert_equal Date.new(2005,2,28), Date.new(2005,2,22).next_week
250270
assert_equal Date.new(2005,3,4), Date.new(2005,2,22).next_week(:friday)
@@ -265,6 +285,10 @@ def test_prev_month_on_31st
265285
assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).prev_month
266286
end
267287

288+
def test_last_month_on_31st
289+
assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).last_month
290+
end
291+
268292
def test_yesterday_constructor
269293
assert_equal Date.current - 1, Date.yesterday
270294
end

activesupport/test/core_ext/date_time_ext_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ def test_prev_year
159159
assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).prev_year
160160
end
161161

162+
def test_last_year
163+
assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).last_year
164+
end
165+
162166
def test_next_year
163167
assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).next_year
164168
end
@@ -232,6 +236,14 @@ def test_prev_week
232236
assert_equal DateTime.civil(2006,11,15), DateTime.civil(2006,11,23,0,0,0).prev_week(:wednesday)
233237
end
234238

239+
def test_last_week
240+
assert_equal DateTime.civil(2005,2,21), DateTime.civil(2005,3,1,15,15,10).last_week
241+
assert_equal DateTime.civil(2005,2,22), DateTime.civil(2005,3,1,15,15,10).last_week(:tuesday)
242+
assert_equal DateTime.civil(2005,2,25), DateTime.civil(2005,3,1,15,15,10).last_week(:friday)
243+
assert_equal DateTime.civil(2006,10,30), DateTime.civil(2006,11,6,0,0,0).last_week
244+
assert_equal DateTime.civil(2006,11,15), DateTime.civil(2006,11,23,0,0,0).last_week(:wednesday)
245+
end
246+
235247
def test_next_week
236248
assert_equal DateTime.civil(2005,2,28), DateTime.civil(2005,2,22,15,15,10).next_week
237249
assert_equal DateTime.civil(2005,3,4), DateTime.civil(2005,2,22,15,15,10).next_week(:friday)
@@ -247,6 +259,10 @@ def test_prev_month_on_31st
247259
assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).prev_month
248260
end
249261

262+
def test_last_month_on_31st
263+
assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).last_month
264+
end
265+
250266
def test_xmlschema
251267
assert_match(/^1880-02-28T15:15:10\+00:?00$/, DateTime.civil(1880, 2, 28, 15, 15, 10).xmlschema)
252268
assert_match(/^1980-02-28T15:15:10\+00:?00$/, DateTime.civil(1980, 2, 28, 15, 15, 10).xmlschema)

activesupport/test/core_ext/time_ext_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ def test_prev_year
198198
assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).prev_year
199199
end
200200

201+
def test_last_year
202+
assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).last_year
203+
end
204+
201205
def test_next_year
202206
assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).next_year
203207
end
@@ -505,6 +509,16 @@ def test_prev_week
505509
assert_equal Time.local(2006,11,15), Time.local(2006,11,23,0,0,0).prev_week(:wednesday)
506510
end
507511
end
512+
513+
def test_last_week
514+
with_env_tz 'US/Eastern' do
515+
assert_equal Time.local(2005,2,21), Time.local(2005,3,1,15,15,10).last_week
516+
assert_equal Time.local(2005,2,22), Time.local(2005,3,1,15,15,10).last_week(:tuesday)
517+
assert_equal Time.local(2005,2,25), Time.local(2005,3,1,15,15,10).last_week(:friday)
518+
assert_equal Time.local(2006,10,30), Time.local(2006,11,6,0,0,0).last_week
519+
assert_equal Time.local(2006,11,15), Time.local(2006,11,23,0,0,0).last_week(:wednesday)
520+
end
521+
end
508522

509523
def test_next_week
510524
with_env_tz 'US/Eastern' do
@@ -662,6 +676,10 @@ def test_prev_month_on_31st
662676
assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).prev_month
663677
end
664678

679+
def test_last_month_on_31st
680+
assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).last_month
681+
end
682+
665683
def test_xmlschema_is_available
666684
assert_nothing_raised { Time.now.xmlschema }
667685
end

railties/guides/source/active_support_core_extensions.textile

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2869,6 +2869,8 @@ d.next_year # => Wed, 28 Feb 2001
28692869

28702870
Active Support defines these methods as well for Ruby 1.8.
28712871

2872+
+prev_year+ is aliased to +last_year+.
2873+
28722874
h6. +prev_month+, +next_month+
28732875

28742876
In Ruby 1.9 +prev_month+ and +next_month+ return the date with the same day in the last or next month:
@@ -2890,6 +2892,8 @@ Date.new(2000, 1, 31).next_month # => Tue, 29 Feb 2000
28902892

28912893
Active Support defines these methods as well for Ruby 1.8.
28922894

2895+
+prev_month+ is aliased to +last_month+.
2896+
28932897
h6. +beginning_of_week+, +end_of_week+
28942898

28952899
The methods +beginning_of_week+ and +end_of_week+ return the dates for the
@@ -2935,6 +2939,8 @@ d.prev_week(:saturday) # => Sat, 01 May 2010
29352939
d.prev_week(:friday) # => Fri, 30 Apr 2010
29362940
</ruby>
29372941

2942+
+prev_week+ is aliased to +last_week+.
2943+
29382944
h6. +beginning_of_month+, +end_of_month+
29392945

29402946
The methods +beginning_of_month+ and +end_of_month+ return the dates for the beginning and end of the month:
@@ -3145,21 +3151,21 @@ end_of_week (at_end_of_week)
31453151
monday
31463152
sunday
31473153
weeks_ago
3148-
prev_week
3154+
prev_week (last_week)
31493155
next_week
31503156
months_ago
31513157
months_since
31523158
beginning_of_month (at_beginning_of_month)
31533159
end_of_month (at_end_of_month)
3154-
prev_month
3160+
prev_month (last_month)
31553161
next_month
31563162
beginning_of_quarter (at_beginning_of_quarter)
31573163
end_of_quarter (at_end_of_quarter)
31583164
beginning_of_year (at_beginning_of_year)
31593165
end_of_year (at_end_of_year)
31603166
years_ago
31613167
years_since
3162-
prev_year
3168+
prev_year (last_year)
31633169
next_year
31643170
</ruby>
31653171

@@ -3321,21 +3327,21 @@ end_of_week (at_end_of_week)
33213327
monday
33223328
sunday
33233329
weeks_ago
3324-
prev_week
3330+
prev_week (last_week)
33253331
next_week
33263332
months_ago
33273333
months_since
33283334
beginning_of_month (at_beginning_of_month)
33293335
end_of_month (at_end_of_month)
3330-
prev_month
3336+
prev_month (last_month)
33313337
next_month
33323338
beginning_of_quarter (at_beginning_of_quarter)
33333339
end_of_quarter (at_end_of_quarter)
33343340
beginning_of_year (at_beginning_of_year)
33353341
end_of_year (at_end_of_year)
33363342
years_ago
33373343
years_since
3338-
prev_year
3344+
prev_year (last_year)
33393345
next_year
33403346
</ruby>
33413347

0 commit comments

Comments
 (0)