I don't think I can count the number of times I have had to do date math based on 'business time' in software engineering projects. By 'business time', I mean calculating durations, event start/end times, and other milestones while avoiding weekends, holidays, and in some cases, taking into account the actual beginning and ending of the workday. For the past couple of years I have envied the date/time functions available in Rails' ActiveSupport library and the ability to say things like "4.days.ago" and "8.hours.from_now" directly in code, so I have been thinking "I have all that date/time math in some old Java libraries - I'd love to do something as slick as ActiveSupport's time functions in Ruby".
I finally got around to doing something about it - a new gem called "business_time". Lets install it:
gem install business_time
Here are some examples that you should be able to copy and paste into a ruby console:
and it all works with any Ruby Date, Time, or DateTime object.
Finally, if you are using it within Rails, it also includes a generator that creates an initializer and a yaml file that includes everything you need to adjust the start and end of the business day, add your own holidays, etc. and have it all properly initialized in your rails environent.
This gem goes great with my state-based workflow gem StonePath, as it is useful for calculating things like tasking due dates.
Awesome library. Have had this need myself and always just hacked the calculations more manually. Having a general purpose library is much nicer.
Posted by: Eric Anderson | May 05, 2010 at 11:58 AM
I can't help thinking of the Flight of the Conchords song "Business Time." http://www.youtube.com/watch?v=WGOohBytKTU
Posted by: Nils Jonsson | May 05, 2010 at 10:44 PM
1. Are the first and last business days of the month available?
2. How are holidays, eg UK Bank Holidays, specified? Are the automatically fetched from a location or do I have to specifiy them?
Thanks
Praful
Posted by: praful | May 16, 2010 at 12:23 PM
That is pretty good. Congratulations. Do you have a boolean, like
t = Time.now
t.open_for_business?
t.closed_for_business?
Also, in the long run, I think you would really want several "business_hour configurations". So you can support different offices in different locations.
Stephan
Posted by: Stephan Wehner | June 15, 2010 at 05:50 PM
Is there a method to compute the number of business days between two dates?
Posted by: NG | September 01, 2010 at 11:33 AM