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

Instance Method Details

#business_time_until(to_time) ⇒ Object



139
140
141
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
# File 'lib/business_time/time_extensions.rb', line 139

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_daysObject



177
178
179
# File 'lib/business_time/time_extensions.rb', line 177

def consecutive_non_working_days
  !workday? ? consecutive_days { |date| !date.workday? } : []
end

#consecutive_workdaysObject



173
174
175
# File 'lib/business_time/time_extensions.rb', line 173

def consecutive_workdays
  workday? ? consecutive_days { |date| date.workday? } : []
end

#during_business_hours?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/business_time/time_extensions.rb', line 169

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.

Returns:

  • (Boolean)


10
11
12
# File 'lib/business_time/time_extensions.rb', line 10

def weekday?
  BusinessTime::Config.weekdays.include?(wday)
end

#workday?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.

Returns:

  • (Boolean)


5
6
7
# File 'lib/business_time/time_extensions.rb', line 5

def workday?
  weekday? && !BusinessTime::Config.holidays.include?(to_date)
end