aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb3
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb7
3 files changed, 11 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 1fe697a227..e24736646f 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fixed Time#at_beginning_of_week returned the next Monday instead of the previous one when called on a Sunday #1403 [jean.helou@gmail.com]
+
* Increased the speed of indifferent hash access by using Hash#default. #1436 [Nicholas Seckar]
* Added that " " is now also blank? (using strip if available)
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb
index 1c9a3523a9..8247003ca3 100644
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -87,7 +87,8 @@ module ActiveSupport #:nodoc:
# Returns a new Time representing the "start" of this week (Monday, 0:00)
def beginning_of_week
- (self - self.wday.days).midnight + 1.day
+ days_to_monday = self.wday!=0 ? self.wday-1 : 6
+ (self - days_to_monday.days).midnight
end
alias :monday :beginning_of_week
alias :at_beginning_of_week :beginning_of_week
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb
index 18ae3219bc..6be1a70f94 100644
--- a/activesupport/test/core_ext/time_ext_test.rb
+++ b/activesupport/test/core_ext/time_ext_test.rb
@@ -13,6 +13,13 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
def test_begining_of_week
assert_equal Time.local(2005,1,31), Time.local(2005,2,4,10,10,10).beginning_of_week
+ assert_equal Time.local(2005,11,28), Time.local(2005,11,28,0,0,0).beginning_of_week #monday
+ assert_equal Time.local(2005,11,28), Time.local(2005,11,29,0,0,0).beginning_of_week #tuesday
+ assert_equal Time.local(2005,11,28), Time.local(2005,11,30,0,0,0).beginning_of_week #wednesday
+ assert_equal Time.local(2005,11,28), Time.local(2005,12,01,0,0,0).beginning_of_week #thursday
+ assert_equal Time.local(2005,11,28), Time.local(2005,12,02,0,0,0).beginning_of_week #friday
+ assert_equal Time.local(2005,11,28), Time.local(2005,12,03,0,0,0).beginning_of_week #saturday
+ assert_equal Time.local(2005,11,28), Time.local(2005,12,04,0,0,0).beginning_of_week #sunday
end
def test_beginning_of_day