Skip to content

Commit

Permalink
Fix 0.business_days and 0.business_hours calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
mlen committed Jun 22, 2017
1 parent ba960ae commit 29f611d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/business_time/business_days.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ def <=>(other)
end

def after(time = Time.current)
positive_days? ? calculate_after(time, @days) : calculate_before(time, -@days)
non_negative_days? ? calculate_after(time, @days) : calculate_before(time, -@days)
end

alias_method :from_now, :after
alias_method :since, :after

def before(time = Time.current)
positive_days? ? calculate_before(time, @days) : calculate_after(time, -@days)
non_negative_days? ? calculate_before(time, @days) : calculate_after(time, -@days)
end

alias_method :ago, :before
alias_method :until, :before

private

def positive_days?
@days > 0
def non_negative_days?
@days >= 0
end

def calculate_after(time, days)
Expand Down
8 changes: 4 additions & 4 deletions lib/business_time/business_hours.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ def from_now
end

def after(time)
positive_hours? ? calculate_after(time, @hours) : calculate_before(time, -@hours)
non_negative_hours? ? calculate_after(time, @hours) : calculate_before(time, -@hours)
end
alias_method :since, :after

def before(time)
positive_hours? ? calculate_before(time, @hours) : calculate_after(time, -@hours)
non_negative_hours? ? calculate_before(time, @hours) : calculate_after(time, -@hours)
end

private

def positive_hours?
@hours > 0
def non_negative_hours?
@hours >= 0
end

def calculate_after(time, hours)
Expand Down
7 changes: 7 additions & 0 deletions test/test_business_days.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
assert_equal expected, after
end

it "should pick next working day when adding zero days on the weekend" do
first = Time.parse("April 10th, 2010, 12:33 pm")
after = 0.business_days.after(first)
expected = Time.parse("April 12th, 2010, 12:33 pm")
assert_equal expected, after
end

it "should move forward one week when adding 5 business days" do
first = Time.parse("April 9th, 2010, 12:33 pm")
after = 5.business_days.after(first)
Expand Down
7 changes: 7 additions & 0 deletions test/test_business_hours.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
assert_equal expected, monday_morning
end

it "picks next working day when adding zero hours on weekends" do
first = Time.parse("April 10th, 2010, 12:33 pm")
after = 0.business_hours.after(first)
expected = Time.parse("April 12th, 2010, 9:00 am")
assert_equal expected, after
end

it "take into account a weekend when adding an hour, using the common interface #since" do
friday_afternoon = Time.parse("April 9th 2010, 4:50 pm")
monday_morning = 1.business_hour.since(friday_afternoon)
Expand Down

0 comments on commit 29f611d

Please sign in to comment.