Module: BusinessTime::TimeExtensions
- Included in:
- ActiveSupport::TimeWithZone, Date, Time
- Defined in:
- lib/business_time/time_extensions.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #business_time_until(to_time, options = {}) ⇒ Object
- #consecutive_non_working_days(options = {}) ⇒ Object
- #consecutive_workdays(options = {}) ⇒ Object
- #during_business_hours?(options = {}) ⇒ Boolean
-
#weekday? ⇒ Boolean
True if this time falls on a weekday.
-
#workday?(options = {}) ⇒ Boolean
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.
Instance Method Details
#business_time_until(to_time, options = {}) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/business_time/time_extensions.rb', line 142 def business_time_until(to_time, ={}) # 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, ) time_b = Time::roll_forward(time_b, ) 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 = {}) ⇒ Object
180 181 182 |
# File 'lib/business_time/time_extensions.rb', line 180 def consecutive_non_working_days(={}) !workday?() ? consecutive_days { |date| !date.workday?() } : [] end |
#consecutive_workdays(options = {}) ⇒ Object
176 177 178 |
# File 'lib/business_time/time_extensions.rb', line 176 def consecutive_workdays(={}) workday?() ? consecutive_days { |date| date.workday?() } : [] end |
#during_business_hours?(options = {}) ⇒ Boolean
172 173 174 |
# File 'lib/business_time/time_extensions.rb', line 172 def during_business_hours?(={}) self.workday?() && self.to_i.between?(Time.beginning_of_workday(self).to_i, Time.end_of_workday(self).to_i) end |
#weekday? ⇒ Boolean
True if this time falls on a weekday.
14 15 16 |
# File 'lib/business_time/time_extensions.rb', line 14 def weekday? BusinessTime::Config.weekdays.include?(wday) end |
#workday?(options = {}) ⇒ Boolean
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.
7 8 9 10 11 |
# File 'lib/business_time/time_extensions.rb', line 7 def workday?(={}) weekday? && !BusinessTime::Config.holidays.include?(to_date) && !to_array_of_dates([:holidays]).include?(to_date) end |