Skip to content

Commit

Permalink
Merge pull request #105 from zendesk/craig/day-periods
Browse files Browse the repository at this point in the history
Add helper for generating periods on a date
  • Loading branch information
craiglittle authored May 19, 2017
2 parents 874860e + 6fcefab commit b8763f6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,21 @@ Biz.time(2, :hours).after(Time.utc(2015, 12, 25, 9, 30))
# Calculations can be performed in seconds, minutes, hours, or days
Biz.time(1, :day).after(Time.utc(2015, 1, 8, 10))

# Find the previous business time
Biz.time(0, :hours).before(Time.utc(2016, 1, 8, 6))

# Find the next business time
Biz.time(0, :hours).after(Time.utc(2016, 1, 8, 20))

# Find the amount of business time between two times
Biz.within(Time.utc(2015, 3, 7), Time.utc(2015, 3, 14)).in_seconds

# Find the start of the business day
Biz.periods.on(Date.today).first.start_time

# Find the end of the business day
Biz.periods.on(Date.today).to_a.last.end_time

# Determine if a time is in business hours
Biz.in_hours?(Time.utc(2015, 1, 10, 9))

Expand Down
8 changes: 8 additions & 0 deletions lib/biz/periods/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ def before(origin)
Before.new(schedule, origin)
end

def on(date)
schedule
.periods
.after(schedule.in_zone.on_date(date, DayTime.midnight))
.timeline
.until(schedule.in_zone.on_date(date, DayTime.endnight))
end

protected

attr_reader :schedule
Expand Down
30 changes: 29 additions & 1 deletion spec/periods/proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
RSpec.describe Biz::Periods::Proxy do
subject(:periods) { described_class.new(schedule) }
let(:hours) {
{
mon: {'09:00' => '17:00'},
tue: {'10:00' => '16:00'},
wed: {'09:00' => '17:00'},
thu: {'10:00' => '12:00', '13:00' => '17:00'},
fri: {'09:00' => '17:00'},
sat: {'11:00' => '14:30'}
}
}

subject(:periods) { described_class.new(schedule(hours: hours)) }

describe '#after' do
let(:origin) { Time.utc(2006, 1, 3) }
Expand Down Expand Up @@ -34,4 +45,21 @@
]
end
end

describe '#on' do
let(:date) { Date.new(2006, 1, 5) }

it 'generates periods on the provided date' do
expect(periods.on(date).to_a).to eq [
Biz::TimeSegment.new(
Time.utc(2006, 1, 5, 10),
Time.utc(2006, 1, 5, 12)
),
Biz::TimeSegment.new(
Time.utc(2006, 1, 5, 13),
Time.utc(2006, 1, 5, 17)
)
]
end
end
end

0 comments on commit b8763f6

Please sign in to comment.