module BusinessTime::TimeExtensions
Public Instance Methods
business_time_until(to_time, options={})
click to toggle source
# File lib/business_time/time_extensions.rb, line 142 def business_time_until(to_time, options={}) # Make sure that we will calculate time from A to B "clockwise" if self < to_time time_a = self time_b = to_time direction = 1 else time_a = to_time time_b = self direction = -1 end # Align both times to the closest business hours time_a = Time::roll_forward(time_a, options) time_b = Time::roll_forward(time_b, options) if time_a.to_date == time_b.to_date time_b - time_a else end_of_workday = Time.end_of_workday(time_a) end_of_workday += 1 if end_of_workday.to_s =~ /23:59:59/ first_day = end_of_workday - time_a days_in_between = ((time_a.to_date + 1)..(time_b.to_date - 1)).sum{ |day| Time::work_hours_total(day) } last_day = time_b - Time.beginning_of_workday(time_b) first_day + days_in_between + last_day end * direction end
consecutive_non_working_days(options={})
click to toggle source
# File lib/business_time/time_extensions.rb, line 180 def consecutive_non_working_days(options={}) !workday?(options) ? consecutive_days { |date| !date.workday?(options) } : [] end
consecutive_workdays(options={})
click to toggle source
# File lib/business_time/time_extensions.rb, line 176 def consecutive_workdays(options={}) workday?(options) ? consecutive_days { |date| date.workday?(options) } : [] end
during_business_hours?(options={})
click to toggle source
# File lib/business_time/time_extensions.rb, line 172 def during_business_hours?(options={}) self.workday?(options) && self.to_i.between?(Time.beginning_of_workday(self).to_i, Time.end_of_workday(self).to_i) end
weekday?()
click to toggle source
True if this time falls on a weekday.
# File lib/business_time/time_extensions.rb, line 14 def weekday? BusinessTime::Config.weekdays.include?(wday) end
workday?(options={})
click to toggle source
True if this time is on a workday (between 00:00:00 and 23:59:59), even if this time falls outside of normal business hours. holidays option allows you to pass in a different Array of holiday dates on each call vs the BusinessTime::Config.holidays which is always static.
# File lib/business_time/time_extensions.rb, line 7 def workday?(options={}) weekday? && !BusinessTime::Config.holidays.include?(to_date) && !to_array_of_dates(options[:holidays]).include?(to_date) end
Private Instance Methods
consecutive_days()
click to toggle source
# File lib/business_time/time_extensions.rb, line 186 def consecutive_days days = [] date = self + 1.day while yield(date) days << date date += 1.day end date = self - 1.day while yield(date) days << date date -= 1.day end (days << self).sort end
to_array_of_dates(values)
click to toggle source
# File lib/business_time/time_extensions.rb, line 201 def to_array_of_dates(values) Array.wrap(values).map(&:to_date) end