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.