Skip to content

Commit

Permalink
covering an edge case on calculating weekend rollovers
Browse files Browse the repository at this point in the history
  • Loading branch information
bokmann committed Apr 25, 2010
1 parent 1ad617b commit 87f191d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/business_time/business_hours.rb
Expand Up @@ -14,6 +14,7 @@ def from_now
end

def after(time)
time = time.roll_forward
@hours.times do
time = time + 1.hour #add an hour

Expand All @@ -29,8 +30,9 @@ def after(time)
end

def before(time)
time = time.roll_forward
@hours.times do
time = time - 1.hour #add an hour
time = time - 1.hour #subtract an hour

if (time < time.beginning_of_workday)
time = time - off_hours # if that pushes us before business hours,
Expand Down
36 changes: 36 additions & 0 deletions lib/extensions/time.rb
Expand Up @@ -15,4 +15,40 @@ def workday?
def weekday?
[1,2,3,4,5].include? self.wday
end


def outsize_business_hours?
before_business_hours? || after_business_hours? || !workday?
end

# rolls forward to the next beginning_of_workday
# when the time is outside of business hours
def roll_forward
next_business_time = self

if (before_business_hours? || !workday?)
next_business_time = beginning_of_workday
end

if after_business_hours?
next_business_time = beginning_of_workday + 1.day
end

while !next_business_time.workday?
next_business_time = next_business_time + 1.day
end

next_business_time
end

private

def before_business_hours?
(self < self.beginning_of_workday)
end

def after_business_hours?
(self > self.end_of_workday)
end

end
17 changes: 17 additions & 0 deletions test/test_business_hours.rb
Expand Up @@ -44,4 +44,21 @@ class TestBusinessHours < Test::Unit::TestCase
assert expected == later
end

should "roll forward to 9 am if asked in the early morning" do
crack_of_dawn_monday = Time.parse("Mon Apr 26, 04:30:00, 2010")
monday_morning = Time.parse("Mon Apr 26, 09:00:00, 2010")
assert_equal monday_morning, crack_of_dawn_monday.roll_forward
end

should "roll forward to the next morning if aftern business hours" do
monday_evening = Time.parse("Mon Apr 26, 18:00:00, 2010")
tuesday_morning = Time.parse("Tue Apr 27, 09:00:00, 2010")
assert_equal tuesday_morning, monday_evening.roll_forward
end

should "consider any time on a weekend as equivalent to monday morning" do
sunday = Time.parse("Sun Apr 25 12:06:56, 2010")
monday = Time.parse("Mon Apr 26, 09:00:00, 2010")
assert_equal 1.business_hour.before(monday), 1.business_hour.before(sunday)
end
end

0 comments on commit 87f191d

Please sign in to comment.