Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix adding dates when starting during the weekend (fixes #14) #16

Merged
merged 1 commit into from Aug 26, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/business_time/business_days.rb
Expand Up @@ -9,10 +9,10 @@ def initialize(days)

def after(time = Time.now)
time = Time.zone ? Time.zone.parse(time.to_s) : Time.parse(time.to_s)
@days.times do
begin
time = time + 1.day
end until Time.workday?(time)
days = @days
while days > 0 || !Time.workday?(time)
days -= 1 if Time.workday?(time)
time = time + 1.day
end
time
end
Expand All @@ -22,10 +22,10 @@ def after(time = Time.now)

def before(time = Time.now)
time = Time.zone ? Time.zone.parse(time.to_s) : Time.parse(time.to_s)
@days.times do
begin
time = time - 1.day
end until Time.workday?(time)
days = @days
while days > 0 || !Time.workday?(time)
days -= 1 if Time.workday?(time)
time = time - 1.day
end
time
end
Expand Down
15 changes: 15 additions & 0 deletions test/test_business_days.rb
Expand Up @@ -64,6 +64,21 @@ class TestBusinessDays < Test::Unit::TestCase
expected = Time.parse("July 5th, 2010, 4:50 pm")
assert_equal expected, monday_afternoon
end

should "move to tuesday if we add one business day during a weekend" do
saturday = Time.parse("April 10th, 2010, 11:00 am")
later = 1.business_days.after(saturday)
expected = Time.parse("April 13th, 2010, 11:00 am")
assert_equal expected, later
end

should "move to thursday if we subtract one business day during a weekend" do
saturday = Time.parse("April 10th, 2010, 11:00 am")
before = 1.business_days.before(saturday)
expected = Time.parse("April 8th, 2010, 11:00 am")
assert_equal expected, before
end

end

end