BusinessPeriod is a ruby library that calculates business period by given hash. This library was designed for lithuanian, latvian and estonian unemployment days.
Add this line to your application's Gemfile:
gem 'business-period'
And then execute:
$ bundle
Or install it yourself as:
$ gem install business-period
There are two ways to initialize:
# config/initializers/business_period.rb
# Set locale to get config file from config/holidays path.
# Set work_days to define which days of week are work days.
locale = 'lt'
work_days = [1, 2, 3, 4, 5]
BusinessPeriod::Config.locale = locale
BusinessPeriod::Config.work_days = work_days
locale = 'lt'
work_days = [1, 2, 3, 4, 5]
BusinessPeriod::Config.locale = -> { locale }
BusinessPeriod::Config.work_days = -> { work_days }
- Dynamically calculates how many days we have to add to period end (Saturdays/Sundays/Holidays).
- Generates new array.
- Extracts weekends and holidays from newly generated array.
- Generates result array.
from = 2
to = 4
# You can pass optional parameters if You want to set primary day as starting point.
# options = { primary_day: Time.now - (3600 * 24) }
# Call BusinessPeriod::Days class to calculate period
BusinessPeriod::Days.call(from, to, options)
Let's say we have no holidays this month and today is Wednesday.
work_days = [1, 2, 3, 4, 5]
(all days except weekends)
from = 2
to = 4
- Begins to count period from the coming day.
- Tomorrow (Thursday) is the first valid day.
- The second valid day will be Fridary (first business day).
- Fourth business day will be Tuesday (Saturday and Sunday are not in scope).
irb(main):001:0> Time.current
=> Wed, 12 Sep 2018 05:49:10 UTC +00:00
irb(main):002:0> from = 2
=> 2
irb(main):003:0> to = 4
=> 4
irb(main):004:0> BusinessPeriod::Days.call(from, to)
=> {:from_date=>Fri, 14 Sep 2018, :to_date=>Tue, 18 Sep 2018]
After checking out the repo, run bin/setup
to install dependencies. Then, run rake
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
The gem is available as open source under the terms of the MIT License.