Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fast_business_time

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast_business_time

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

FastBusinessTime

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

Installation

Add this line to your application's Gemfile:

gem 'fast_business_time'

And then execute:

$ bundle

Or install it yourself as:

$ gem install fast_business_time

Usage

Initializiation

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

schedule = {
  [:mon, :tue, :wed, :thu, :fri] => [[9 * 3600, 17 * 3600]]
}
holidays = [Date.new(2016, 1, 1), Date.new(2016, 12, 25)]
calculator = FastBusinessTime::Calculator.new(schedule: 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 ||= FastBusinessTime::Calculator.new(
	          schedule: [:mon, :tue, :wed, :thu, :fri] => [[9 * 3600, 17 * 3600]],
	          holidays: [Date.new(2016, 1, 1), Date.new(2016, 12, 25)]
	        )
  end

  def self.sf
  	@sf ||= FastBusinessTime::Calculator.new(
	          schedule: [:mon, :tue, :wed, :thu, :fri] => [[9 * 3600, 18 * 3600]],
	          holidays: [Date.new(2016, 1, 1), Date.new(2016, 12, 25)]
	        )
  end
end
Methods
  • 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.
  • calculator.holiday?(time_or_date): (Boolean) Whether time or date is a holiday.
  • calculator.add_days_to_date(days, date): (Date) Adds given work days to a date.

Development

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 rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/spreemo/fast_business_time. 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.

License

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

FAQs

Package last updated on 01 Dec 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc