1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Added next_week and made beginning_of_week be a Monday, not a Sunday

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@733 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2005-02-21 14:46:33 +00:00
parent 637642c8b8
commit 25f8a25c3e
2 changed files with 16 additions and 4 deletions

View file

@ -53,13 +53,19 @@ module ActiveSupport #:nodoc:
end end
end end
# Returns a new Time representing the "start" of this week (sunday, 0:00) # Returns a new Time representing the "start" of this week (Monday, 0:00)
def beginning_of_week def beginning_of_week
(self - self.wday.days).midnight (self - self.wday.days).midnight + 1.day
end end
alias :sunday :beginning_of_week alias :monday :beginning_of_week
alias :at_beginning_of_week :beginning_of_week alias :at_beginning_of_week :beginning_of_week
# Returns a new Time representing the start of the given day in next week (default is Monday).
def next_week(day = :monday)
days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
since(1.week).beginning_of_week.since(days_into_week[day].day).change(:hour => 0)
end
# Returns a new Time representing the start of the day (0:00) # Returns a new Time representing the start of the day (0:00)
def beginning_of_day def beginning_of_day
self - self.seconds_since_midnight self - self.seconds_since_midnight

View file

@ -12,7 +12,7 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
end end
def test_begining_of_week def test_begining_of_week
assert_equal Time.local(2005,1,30), Time.local(2005,2,4,10,10,10).beginning_of_week assert_equal Time.local(2005,1,31), Time.local(2005,2,4,10,10,10).beginning_of_week
end end
def test_beginning_of_day def test_beginning_of_day
@ -80,4 +80,10 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
assert_equal Time.utc(2005,2,22,16,45), Time.utc(2005,2,22,15,15,10).change(:hour => 16, :min => 45) assert_equal Time.utc(2005,2,22,16,45), Time.utc(2005,2,22,15,15,10).change(:hour => 16, :min => 45)
assert_equal Time.utc(2005,2,22,15,45), Time.utc(2005,2,22,15,15,10).change(:min => 45) assert_equal Time.utc(2005,2,22,15,45), Time.utc(2005,2,22,15,15,10).change(:min => 45)
end end
def test_next_week
assert_equal Time.local(2005,2,28), Time.local(2005,2,22,15,15,10).next_week
assert_equal Time.local(2005,2,29), Time.local(2005,2,22,15,15,10).next_week(:tuesday)
assert_equal Time.local(2005,3,4), Time.local(2005,2,22,15,15,10).next_week(:friday)
end
end end