Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support 11pm close times #230

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions lib/business_time/business_hours.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ def calculate_after(time, hours, options={})
after_time = Time.roll_forward(time, options)
# Step through the hours, skipping over non-business hours
hours.times do
eod = Time.end_of_workday(after_time)
after_time = after_time + 1.hour

if after_time.hour == 0 && after_time.min == 0 && after_time.sec == 0
after_time = Time.roll_forward(after_time, options)
elsif (after_time > Time.end_of_workday(after_time))
# Ignore hours before opening and after closing
delta = after_time - Time.end_of_workday(after_time)
# Ignore hours before opening and after closing
if after_time > eod
delta = after_time - eod

# Handle errors due to XX:59:59 exceptions
delta = 0 if delta == 1.second

after_time = Time.roll_forward(after_time, options) + delta
end

Expand All @@ -64,13 +67,12 @@ def calculate_before(time, hours, options={})
before_time = Time.roll_backward(time)
# Step through the hours, skipping over non-business hours
hours.times do
bod = Time.beginning_of_workday(before_time)
before_time = before_time - 1.hour

if before_time.hour == 0 && before_time.min == 0 && before_time.sec == 0
before_time = Time.roll_backward(before_time - 1.second, options)
elsif (before_time <= Time.beginning_of_workday(before_time))
# Ignore hours before opening and after closing
delta = Time.beginning_of_workday(before_time) - before_time
# Ignore hours before opening and after closing
if before_time <= bod
delta = bod - before_time

# Due to the 23:59:59 end-of-workday exception
time_roll_backward = Time.roll_backward(before_time, options)
Expand Down
13 changes: 13 additions & 0 deletions test/test_business_hours.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@
assert_equal friday, 24.business_hours.before(monday)
end

it "respect work_hours with 11pm closing time" do
two_before_close = Time.parse("2024-09-30 21:00")
two_after_open = Time.parse("2024-10-01 16:00")

BusinessTime::Config.work_hours = {
mon: ["14:00", "23:00"],
tue: ["14:00", "23:00"],
}

assert_equal two_after_open, 4.business_hours.after(two_before_close)
assert_equal two_before_close, 4.business_hours.before(two_after_open)
end

it "respect work_hours within same day" do
friday_start = Time.parse("December 24, 2010 8:00")
friday_end = Time.parse("December 24, 2010 17:00")
Expand Down