Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #16 from drogus/days-after-weekend

Fix adding dates when starting during the weekend (fixes #14)
  • Loading branch information...
commit 3b135be4dacec85abf59113fc1f91a5fb7652352 2 parents 6742b7f + d6f657c
@bokmann authored
Showing with 23 additions and 8 deletions.
  1. +8 −8 lib/business_time/business_days.rb
  2. +15 −0 test/test_business_days.rb
View
16 lib/business_time/business_days.rb
@@ -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
@@ -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
View
15 test/test_business_days.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.