Created
April 25, 2010 22:12
-
-
Save bokmann/378770 to your computer and use it in GitHub Desktop.
text for my blog entry showing how to use business_time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# require the libraries to make it all work | |
require 'rubygems' | |
require 'active_support' | |
require 'business_time' | |
# What time is it now? Of course, this will be different | |
# for you, so your relative answrs below will vary | |
# accordingly: | |
Time.now | |
# If you were to call: | |
4.business_hours.from_now | |
# on a Monday morning, it'll return a time on Monday | |
# afternoon. | |
# Monday afternoon, it would return sometime on Tuesday | |
# morning. | |
# On Friday afternoon, it would return Monday morning, | |
# unless that Monday were a holiday, in which case it | |
# would return Tuesday morning. | |
# It works calculating backwards too, by asking for | |
# values like: | |
6.business_hours.ago | |
# It also calculates based on days: | |
5.business_days.from_now | |
# and you can base the calculations on any date/time, | |
# using the same helper names that ActiveSupport gives | |
# you: | |
my_birthday = Date.parse("August 4th, 2010") | |
13.business_days.since(my_birthday) | |
7.business_days.until(my_birthday) | |
# I have always found the ActiveSupport helper names | |
# slightly awkward, so I have also provided some of my | |
# own: | |
6.business_days.after(my_birthday) | |
10.business_days.before(my_birthday) | |
# By default, the range of business hours is from | |
# 9:00am, to 5:00pm, but you can change this easily: | |
BusinessTime::Config.beginning_of_workday = "8:30am" | |
BusinessTime::Config.end_of_workday = "5:30pm" | |
# and you can add holidays easily: | |
BusinessTime::Config.holidays << my_birthday | |
6.business_hours.after(Time.parse("August 3rd, 3:00pm")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I recently had to change
require 'active_support'
to
require 'active_support/core_ext'
in order for this gist to work properly.