Permalink
Browse files

Fixed Time#at_beginning_of_week returned the next Monday instead of t…

…he previous one when called on a Sunday #1403 [jean.helou@gmail.com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1431 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 4351419 commit a3659d583586d45c5f48fdd103ff8c797cecfafe @dhh dhh committed Jun 16, 2005
View
2 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)
View
3 activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -87,7 +87,8 @@ def next_month
# 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
View
7 activesupport/test/core_ext/time_ext_test.rb
@@ -13,6 +13,13 @@ def test_seconds_since_midnight
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

0 comments on commit a3659d5

Please sign in to comment.