No commit activity in last 3 years
No release in over 3 years
Have you ever wanted to do things like "6.business_days.from_now" and have weekends and holidays taken into account? Now you can.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

business_time¶ ↑

ActiveSupport gives us some great helpers so we can do things like:

5.days.ago

and

8.hours.from_now

as well as helpers to do that from any provided date or time.

I needed this, but taking into account business hours/days and holidays.

Usage¶ ↑

  • install the gem

    gem install business_time
    

    or

    sudo gem install business_time
    

    if you require sudo to install gems

  • open up your console

    # if in irb, add these lines:
    
    require 'rubygems'
    require 'active_support'
    require 'business_time'
    
    # try these examples, using the current time:
    
    1.business_hour.from_now
    4.business_hours.from_now
    8.business_hours.from_now
    
    1.business_hour.ago
    4.business_hours.ago
    8.business_hours.ago
    
    1.business_day.from_now
    4.business_days.from_now
    8.business_days.from_now
    
    1.business_day.ago
    4.business_days.ago
    8.business_days.ago
    
    # and we can do it from any Date or Time object.
    my_birthday = Date.parse("August 4th, 1969")
    8.business_days.after(my_birthday)
    8.business_days.before(my_birthday)
    
    my_birthday = Time.parse("August 4th, 1969, 8:32 am")
    8.business_days.after(my_birthday)
    8.business_days.before(my_birthday)
    
    # 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)
    

Usage in Rails¶ ↑

The code above should work on a rails console without any issue. You will want to add a line something like:

config.gem "business_time"

to your environment.rb file. Or if you’re using bundler, add this line to your Gemfile:

gem "business_time"

This gem also includes a generator so you can bootstrap some stuff in your environment:

./script/generate business_time_config

Or in Rails 3:

script/rails generate business_time:config

The generator will add a /config/business_time.yml and a /config/initializers/business_time.rb file that will cause the start of business day, the end of business day, and your holidays to be loaded from the yaml file. You might want to programatically load your holidays from a database table, but you will want to pay attention to how the initializer works - you will want to make sure that the initializer sets stuff up appropriately so rails instances on mongrels or passenger will have the appropriate data as they come up and down.

Outside of Rails¶ ↑

This code does depend on ActiveSupport, but nothing else within rails. Even then, it would be pretty easy to break that dependency as well (but would add some code bloat and remove some clarity). Feel free to use it on any ruby project you’d like!

Timezone support¶ ↑

This gem strives to be timezone-agnostic. Due to some complications in the handling of timezones in the built in Time class, and some complexities (bugs?) in the timeWithZone class, this was harder than expected… but here’s the idea:

  • When you configure the gem with something like 9:00am as the start time, this is agnostic of time zone.

  • When you are dealing with a Time or TimeWithZone class, the timezone is preserved and the beginning and end of times for the business day are referenced in that time zone.

This can lead to some wierd looking effects if, say, you are in the Eastern time zone but doing everything in UTC times… Your business day will appear to start and end at 9:00 and 5:00 UTC. If this seems perplexing to you, I can almost guarantee you are in over your head with timezones in other ways too, this is just the first place you encountered it. Timezone relative date handling gets more and more complicated every time you look at it and takes a long time before it starts to seem simple again. I’m hoping Arild and I write some good blog entries on the subject at blog.codesherpas.com.

Contributors¶ ↑

* David Bock  http://github.com/bokmann
* Enrico Bianco  http://github.com/enricob
* Arild Shirazi  http://github.com/ashirazi

(Special thanks for Arild on the complexities of dealing with TimeWithZone)

Note on Patches/Pull Requests¶ ↑

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

TODO¶ ↑

  • if it doesn’t pollute the logic too much, I’d like to vary the days counted as ‘business days’. Bakers often don’t work on Mondays, for instance. I’d do it in something like this:

    BusinessTime::Config.work_week = [:tue, :wed, :thur, :fri, :sat]
    
  • Arild has pointed out that there may be some logical inconsistencies regaring the beginning_of_workday and end_of workday times not actually being considered inside of the workday. I’d like to make sure that they work as if the beginning_of_workday is included and the end_of_workday is not included, just like the ‘…’ range operator in Ruby.

NOT TODO¶ ↑

  • I spent way too much time in my previous java-programmer life building frameworks that worshipped complexity, always trying to give the developer-user ultimate flexibility at the expense of the ‘surface area’ of the api. Never again - I will sooner limit functionality to 80% so that something stays usable and let people fork.

  • While there have been requests to add ‘business minutes’ and even ‘business seconds’ to this gem, I won’t entertain a pullup request with such things. If you find it useful, great. Most users won’t, and they don’t need the baggage.

  • I’m torn on the inclusion of some kind of business_duration_between two Times, for the following reasons:

    • As a duration, it will be in seconds. This would naturally be business-seconds, which I trimmed out above.

    • The logic is complex and error prone.

    • There is no logical place to put it, as most of this gem mirrors the Timehelpers in ActiveSupport.

    As a result I’m unlikely to add it unless I have an epiphany or someone wows me with an awesome pullup request.

Copyright © 2010 bokmann. See LICENSE for details.