Inspired by business_time gem, speed is the focus of this separate implementation to execute time calculations given business hours.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Time calculations based on business hours. Inspired by business_time but with significantly better performance.


Add this line to your application's Gemfile:

gem 'operating_hours'

And then execute:

$ bundle

Or install it yourself as:

$ gem install operating_hours



First initialize a calculator, a calculator needs a schedule and holidays:

schedule = {
  [:mon, :tue, :wed, :thu, :fri] => [[9 * 3600, 17 * 3600]]
holidays = [, 1, 1),, 12, 25)]
calculator = schedule, holidays: holidays)

schedule is a hash whose keys are an array of weekdays and the values are an array of time intervals in seconds since the beginning of the day. So if we have a business that works on Monday 9:00-13:00, 14:00-17:30 and Tue-Fri 9:00-17:00 we would set schedule to:

schedule = {
  [:mon] => [[9 * 3600, 13 * 3600], [14 * 3600, 17 * 3600 + 30 * 60]],
  [:tue, :wed, :thu, :fri] => [[9 * 3600, 17 * 3600]]]

holidays is an array of dates, you could probably use the holiday gem

It's recommended to memoize the calculators, so you could do:

class TimeCalculators
  def self.ny
	@ny ||=
	          schedule: [:mon, :tue, :wed, :thu, :fri] => [[9 * 3600, 17 * 3600]],
	          holidays: [, 1, 1),, 12, 25)]

  def self.sf
  	@sf ||=
	          schedule: [:mon, :tue, :wed, :thu, :fri] => [[9 * 3600, 18 * 3600]],
	          holidays: [, 1, 1),, 12, 25)]


  • calculator.seconds_between_times(time1, time2): (Integer) Working seconds between two times.
  • calculator.days_between_dates(date1, date2): (Integer) Number of work days between two dates (excluding edges).
  • calculator.seconds_since_beginning_of_workday(time): (Integer) Working seconds since workday started.
  • calculator.seconds_until_end_of_workday(time): (Integer) Working seconds until workday finishes.
  • (Boolean) Whether time or date is a holiday.
  • calculator.add_days_to_date(days, date): (Date) Adds given work days to a date.


After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.


Copyright (c) 2016 Spreemo. The gem is available as open source under the terms of the MIT License.