Skip to content

Commit

Permalink
Fix adding dates when starting during the weekend (fixes bokmann#14)
Browse files Browse the repository at this point in the history
When adding days to a date during a weekend, starting date
should be treated as first day after or last day before the weekend.
  • Loading branch information
drogus committed Apr 16, 2012
1 parent 49e9db7 commit d6f657c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
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

0 comments on commit d6f657c

Please sign in to comment.