Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Business date/holiday handling

Tags:

date

ruby

I've posted this question for C# but I may be working in Ruby instead. So I'm asking the same question about Ruby:

I'm looking for a Ruby class/library/module that works similarly to the Perl module Date::Manip as far as business/holiday dates. Using that module in Perl, I can pass it a date and find out whether it's a business day (ie, Mon-Fri) or a holiday. Holidays are very simple to define in a config file (see Date::Manip::Holidays). You can enter a 'fixed' date that applies to every year like:

12/25                           = Christmas

or 'dynamic' dates for every year like:

last Monday in May              = Memorial Day

or 'fixed' dates for a given year like:

5/22/2010                       = Bob's Wedding

You can also pass in a date and get back the next/previous business day (which is any day that's not a weekend and not a holiday).

Does anyone know of anything like that in the Ruby world?

like image 493
DaveKub Avatar asked Sep 29 '10 12:09

DaveKub


2 Answers

You may use the holidays-gem. http://rubygems.org/gems/holidays

Some national (and regional) holidays are already predefined, you may define your own holiday definitions.

like image 137
knut Avatar answered Sep 30 '22 01:09

knut


The business_time gem should do what you need.

The example at bottom of the README doc is a good starting example:

require 'rubygems'
require 'active_support'
require 'business_time'

# We can adjust the start and end time of our business hours
BusinessTime::Config.beginning_of_workday = "8:30 am"
BusinessTime::Config.end_of_workday = "5:30 pm"

# and we can add holidays that don't count as business days
# July 5 in 2010 is a monday that the U.S. takes off because 
# our independence day falls on that Sunday.
three_day_weekend = Date.parse("July 5th, 2010")
BusinessTime::Config.holidays << three_day_weekend
friday_afternoon = Time.parse("July 2nd, 2010, 4:50 pm")
tuesday_morning = 1.business_hour.after(friday_afternoon)

You probably going to need the chronic gem to help you build the holiday dates from your config file. However YMMV because your example last monday in may doesn't work in chronic. Hackaround is do something like this:

# last monday in May (2010)
Chronic.parse('last monday', :now => Time.parse('2010-06-01'))

And look at the tickle gem which works on top of chronic for a way to add recurring events.

/I3az/

like image 27
draegtun Avatar answered Sep 29 '22 23:09

draegtun