Skip to content

Commit

Permalink
Merge pull request #88 from mcurtis/develop
Browse files Browse the repository at this point in the history
Adding a method to determine if a time is within business hours
  • Loading branch information
bokmann committed Nov 3, 2014
2 parents 0a45c4a + 6007e0f commit 1e0ad9e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.rdoc
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions lib/business_time/time_extensions.rb
Expand Up @@ -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
5 changes: 5 additions & 0 deletions test/test_time_extensions.rb
Expand Up @@ -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 ======================

Expand Down

0 comments on commit 1e0ad9e

Please sign in to comment.