diff options
4 files changed, 58 insertions, 12 deletions
diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 0c4081aa8d..f0f67765c6 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -190,20 +190,30 @@ class Date result = self - days_to_start acts_like?(:time) ? result.midnight : result end - alias :monday :beginning_of_week alias :at_beginning_of_week :beginning_of_week - # Returns a new +Date+/+DateTime+ representing the end of this week, week - # starts on +start_day+, default is +:monday+. +DateTime+ objects have their - # time set to 23:59:59). + # Returns a new +Date+/+DateTime+ representing the start of this week. Week is + # assumed to start on a Monday. +DateTime+ objects have their time set to 0:00. + def monday + beginning_of_week + end + + # Returns a new +Date+/+DateTime+ representing the end of this week. Week is + # assumed to start on +start_day+, default is +:monday+. +DateTime+ objects + # have their time set to 23:59:59. def end_of_week(start_day = :monday) days_to_end = 6 - days_to_week_start(start_day) result = self + days_to_end.days self.acts_like?(:time) ? result.end_of_day : result end - alias :sunday :end_of_week alias :at_end_of_week :end_of_week + # Returns a new +Date+/+DateTime+ representing the end of this week. Week is + # assumed to start on a Monday. +DateTime+ objects have their time set to 23:59:59. + def sunday + end_of_week + end + # Returns a new +Date+/+DateTime+ representing the given +day+ in the previous # week. Default is +:monday+. +DateTime+ objects have their time set to 0:00. def prev_week(day = :monday) diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 2cc5c82265..f3235d11bb 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -174,9 +174,14 @@ class Time days_to_start = days_to_week_start(start_day) (self - days_to_start.days).midnight end - alias :monday :beginning_of_week alias :at_beginning_of_week :beginning_of_week + # Returns a new +Date+/+DateTime+ representing the start of this week. Week is + # assumed to start on a Monday. +DateTime+ objects have their time set to 0:00. + def monday + beginning_of_week + end + # Returns a new Time representing the end of this week, week starts on start_day (default is :monday, i.e. end of Sunday). def end_of_week(start_day = :monday) days_to_end = 6 - days_to_week_start(start_day) @@ -184,6 +189,12 @@ class Time end alias :at_end_of_week :end_of_week + # Returns a new +Date+/+DateTime+ representing the end of this week. Week is + # assumed to start on a Monday. +DateTime+ objects have their time set to 23:59:59. + def sunday + end_of_week + end + # Returns a new Time representing the start of the given day in the previous week (default is :monday). def prev_week(day = :monday) ago(1.week).beginning_of_week.since(DAYS_INTO_WEEK[day].day).change(:hour => 0) diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index b4f848cd44..c040d86327 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -57,6 +57,16 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(2005,11,28), Date.new(2005,12,04).beginning_of_week #sunday end + def test_monday + assert_equal Date.new(2005,11,28), Date.new(2005,11,28).monday + assert_equal Date.new(2005,11,28), Date.new(2005,12,01).monday + end + + def test_sunday + assert_equal Date.new(2008,3,2), Date.new(2008,3,02).sunday + assert_equal Date.new(2008,3,2), Date.new(2008,2,29).sunday + end + def test_beginning_of_week_in_calendar_reform assert_equal Date.new(1582,10,1), Date.new(1582,10,15).beginning_of_week #friday end diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index d601e9ea29..2dee440b3b 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -3041,7 +3041,7 @@ h6. +beginning_of_week+, +end_of_week+ The methods +beginning_of_week+ and +end_of_week+ return the dates for the beginning and end of the week, respectively. Weeks are assumed to start on -Monday, but that can be changed passing an argument, see examples: +Monday, but that can be changed passing an argument. <ruby> d = Date.new(2010, 5, 8) # => Sat, 08 May 2010 @@ -3051,7 +3051,18 @@ d.end_of_week # => Sun, 09 May 2010 d.end_of_week(:sunday) # => Sat, 08 May 2010 </ruby> -+beginning_of_week+ is aliased to +monday+ and +at_beginning_of_week+. +end_of_week+ is aliased to +sunday+ and +at_end_of_week+. ++beginning_of_week+ is aliased to +at_beginning_of_week+ and +end_of_week+ is aliased to +at_end_of_week+. + +h6. +monday+, +sunday+ + +The methods +monday+ and +sunday+ return the dates for the beginning and +end of the week, respectively. Weeks are assumed to start on Monday. + +<ruby> +d = Date.new(2010, 5, 8) # => Sat, 08 May 2010 +d.monday # => Mon, 03 May 2010 +d.sunday # => Sun, 09 May 2010 +</ruby> h6. +prev_week+, +next_week+ @@ -3276,8 +3287,10 @@ The class +DateTime+ is a subclass of +Date+ so by loading +active_support/core_ <ruby> yesterday tomorrow -beginning_of_week (monday, at_beginning_of_week) -end_on_week (at_end_of_week) +beginning_of_week (at_beginning_of_week) +end_of_week (at_end_of_week) +monday +sunday weeks_ago prev_week next_week @@ -3450,8 +3463,10 @@ ago since (in) beginning_of_day (midnight, at_midnight, at_beginning_of_day) end_of_day -beginning_of_week (monday, at_beginning_of_week) -end_on_week (at_end_of_week) +beginning_of_week (at_beginning_of_week) +end_of_week (at_end_of_week) +monday +sunday weeks_ago prev_week next_week |