aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activesupport/CHANGELOG.md3
-rw-r--r--activesupport/lib/active_support/core_ext/date/calculations.rb19
-rw-r--r--railties/guides/source/active_support_core_extensions.textile4
3 files changed, 18 insertions, 8 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 18a115b369..465b0c1e16 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,5 +1,8 @@
## Rails 3.2.0 (unreleased) ##
+* (Date|DateTime|Time)#beginning_of_week accept an optional argument to
+ be able to set the day at which weeks are assumed to start.
+
* Deprecated ActiveSupport::MessageEncryptor#encrypt and decrypt. *José Valim*
* ActiveSupport::Notifications.subscribed provides subscriptions to events while a block runs. *fxn*
diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb
index ea9a1c2c0d..0c4081aa8d 100644
--- a/activesupport/lib/active_support/core_ext/date/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -174,24 +174,28 @@ class Date
months_since(1)
end unless method_defined?(:next_month)
- # Returns number of days to start of this week, week starts on start_day (default is :monday).
+ # Returns number of days to start of this week. Week is assumed to start on
+ # +start_day+, default is +:monday+.
def days_to_week_start(start_day = :monday)
start_day_number = DAYS_INTO_WEEK[start_day]
current_day_number = wday != 0 ? wday - 1 : 6
- days_span = current_day_number - start_day_number
- days_span >= 0 ? days_span : 7 + days_span
+ (current_day_number - start_day_number) % 7
end
- # Returns a new Date/DateTime representing the "start" of this week, week starts on start_day (default is :monday, i.e. Monday; DateTime objects will have time set to 0:00).
+ # Returns a new +Date+/+DateTime+ representing the start of this week. Week is
+ # assumed to start on +start_day+, default is +:monday+. +DateTime+ objects
+ # have their time set to 0:00.
def beginning_of_week(start_day = :monday)
days_to_start = days_to_week_start(start_day)
result = self - days_to_start
- self.acts_like?(:time) ? result.midnight : result
+ 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, i.e. Sunday, DateTime objects will have time set to 23:59:59).
+ # 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).
def end_of_week(start_day = :monday)
days_to_end = 6 - days_to_week_start(start_day)
result = self + days_to_end.days
@@ -200,7 +204,8 @@ class Date
alias :sunday :end_of_week
alias :at_end_of_week :end_of_week
- # Returns a new Date/DateTime representing the start of the given day in the previous week (default is :monday).
+ # 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)
result = (self - 7).beginning_of_week + DAYS_INTO_WEEK[day]
self.acts_like?(:time) ? result.change(:hour => 0) : result
diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile
index c6130f4f62..09f931050d 100644
--- a/railties/guides/source/active_support_core_extensions.textile
+++ b/railties/guides/source/active_support_core_extensions.textile
@@ -3039,7 +3039,9 @@ Active Support defines these methods as well for Ruby 1.8.
h6. +beginning_of_week+, +end_of_week+
-The methods +beginning_of_week+ and +end_of_week+ receive a symbol with a day name in English (in lowercase, default is :monday) and return the dates for the beginning and end of week, assuming weeks start on day, passed as parameter:
+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:
<ruby>
d = Date.new(2010, 5, 8) # => Sat, 08 May 2010