Skip to content

Commit

Permalink
Resolution attempt for issues bokmann#36, bokmann#45 and bokmann#83. …
Browse files Browse the repository at this point in the history
…Ensure we wind to beginning of business day when using business_hours before/after methods
  • Loading branch information
dugjason committed Oct 19, 2015
1 parent 81788e0 commit 812482e
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/business_time/business_days.rb
Expand Up @@ -4,7 +4,7 @@ module BusinessTime
class BusinessDays
include Comparable
attr_reader :days

def initialize(days)
@days = days
end
Expand All @@ -15,13 +15,18 @@ def <=>(other)
end
self.days <=> other.days
end

def after(time = Time.current)
days = @days
while days > 0 || !time.workday?
days -= 1 if time.workday?
time = time + 1.day
end
# If we have a Time or DateTime object, we can roll_forward to the
# beginning of the next business day
if time.is_a?(Time) || time.is_a?(DateTime)
time = Time.roll_forward(time) unless time.during_business_hours?
end
time
end

Expand All @@ -34,9 +39,16 @@ def before(time = Time.current)
days -= 1 if time.workday?
time = time - 1.day
end
# If we have a Time or DateTime object, we can roll_backward to the
# beginning of the previous business day
if time.is_a?(Time) || time.is_a?(DateTime)
unless time.during_business_hours?
time = Time.beginning_of_workday(Time.roll_backward(time))
end
end
time
end

alias_method :ago, :before
alias_method :until, :before
end
Expand Down

0 comments on commit 812482e

Please sign in to comment.