Created
March 19, 2009 15:37
-
-
Save jnewland/81882 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Date | |
def self.next_weekday(original_date=Date.today) | |
weekdays_from(original_date, 1) | |
end | |
def self.weekdays_since(original_date, now=Date.today) | |
weekdays = 1..5 | |
cursor = original_date | |
weekdays_skipped = 0 | |
until cursor == now do | |
cursor += 1 | |
weekdays_skipped += 1 if weekdays.member?(cursor.wday) | |
end | |
weekdays_skipped | |
end | |
def self.weekdays_from(original_date=Date.today, num=1) | |
weekdays = 1..5 | |
cursor = original_date | |
weekdays_skipped = 0 | |
until weekdays_skipped == num do | |
cursor += 1 | |
weekdays_skipped += 1 if weekdays.member?(cursor.wday) | |
end | |
cursor | |
end | |
end | |
if $0 == __FILE__ | |
%w( rubygems test/spec ).each { |f| require f } | |
context "The next weekday" do | |
setup do | |
@thursday = Date.parse('03/19/2009') | |
@friday = Date.parse('03/20/2009') | |
@monday = Date.parse('03/23/2009') | |
end | |
specify "after thursday is friday" do | |
Date.next_weekday(@thursday).to_s.should == @friday.to_s | |
Date.weekdays_since(@thursday, @friday).should == 1 | |
end | |
specify "after friday is monday" do | |
Date.next_weekday(@friday).to_s.should == @monday.to_s | |
Date.weekdays_since(@friday, @monday).should == 1 | |
end | |
end | |
context "2 weekdays" do | |
setup do | |
@thursday = Date.parse('03/19/2009') | |
@friday = Date.parse('03/20/2009') | |
@monday = Date.parse('03/23/2009') | |
@tuesday = Date.parse('03/24/2009') | |
end | |
specify "from thursday is monday" do | |
Date.weekdays_from(@thursday, 2).to_s.should == @monday.to_s | |
Date.weekdays_since(@thursday, @monday).should == 2 | |
end | |
specify "from friday is tuesday" do | |
Date.weekdays_from(@friday, 2).to_s.should == @tuesday.to_s | |
Date.weekdays_since(@friday, @tuesday).should == 2 | |
end | |
end | |
context "3 weekdays" do | |
setup do | |
@thursday = Date.parse('03/19/2009') | |
@friday = Date.parse('03/20/2009') | |
@tuesday = Date.parse('03/24/2009') | |
@wednesday = Date.parse('03/25/2009') | |
end | |
specify "from thursday is tuesday" do | |
Date.weekdays_from(@thursday, 3).to_s.should == @tuesday.to_s | |
Date.weekdays_since(@thursday, @tuesday).should == 3 | |
end | |
specify "from friday is wednesday" do | |
Date.weekdays_from(@friday, 3).to_s.should == @wednesday.to_s | |
Date.weekdays_since(@friday, @wednesday).should == 3 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment