diff --git a/README.rdoc b/README.rdoc index dd1ff8f..ba6f8aa 100644 --- a/README.rdoc +++ b/README.rdoc @@ -89,6 +89,9 @@ I needed this, but taking into account business hours/days and holidays. ticket_reported = Time.parse("February 3, 2012, 10:40 am") ticket_resolved = Time.parse("February 4, 2012, 10:50 am") ticket_reported.business_time_until(ticket_resolved) #=> 8.hours + 10.minutes + + # you can also determine if a given time is within business hours + Time.parse("February 3, 2012, 10:00 am").during_business_hours? # note that counterintuitively, durations might not be quite what you expect when involving weekends. # Consider the following example: @@ -162,6 +165,7 @@ I'm hoping Arild and I write some good blog entries on the subject at http://blo * Piotr Jakubowski http://github.com/piotrj * Glenn Vanderburg http://github.com/glv * Michael Grosser http://github.com/grosser + * Michael Curtis http://github.com/mcurtis (Special thanks for Arild on the complexities of dealing with TimeWithZone) diff --git a/lib/business_time/time_extensions.rb b/lib/business_time/time_extensions.rb index 3f88574..b94d656 100644 --- a/lib/business_time/time_extensions.rb +++ b/lib/business_time/time_extensions.rb @@ -141,5 +141,9 @@ def business_time_until(to_time) first_day + days_in_between + last_day end * direction end + + def during_business_hours? + Time.workday?(self) && self.to_i.between?(Time.beginning_of_workday(self).to_i, Time.end_of_workday(self).to_i) + end end end diff --git a/test/test_time_extensions.rb b/test/test_time_extensions.rb index fa2a76b..f705377 100644 --- a/test/test_time_extensions.rb +++ b/test/test_time_extensions.rb @@ -88,6 +88,11 @@ ticket_resolved = Time.parse("February 4, 2012, 10:40 am") #will roll over to Monday morning, 9:00am assert_equal ticket_reported.business_time_until(ticket_resolved), 6.hours + 20.minutes end + + it "knows if within business hours" do + assert(Time.parse("2013-02-01 10:00").during_business_hours?) + assert(!Time.parse("2013-02-01 5:00").during_business_hours?) + end # =================== .roll_backward ======================