module BusinessTime::TimeExtensions::ClassMethods

Public Instance Methods

after_business_hours?(time) click to toggle source
# File lib/business_time/time_extensions.rb, line 54
def after_business_hours?(time)
  time.to_i > end_of_workday(time).to_i
end
before_business_hours?(time) click to toggle source
# File lib/business_time/time_extensions.rb, line 50
def before_business_hours?(time)
  time.to_i < beginning_of_workday(time).to_i
end
beginning_of_workday(day) click to toggle source

Gives the time at the beginning of the workday, assuming that this time falls on a workday. Note: It pretends that this day is a workday whether or not it really is a workday.

# File lib/business_time/time_extensions.rb, line 32
def beginning_of_workday(day)
  beginning_of_workday = BusinessTime::Config.beginning_of_workday(day)
  change_business_time(day,beginning_of_workday.hour,beginning_of_workday.min,beginning_of_workday.sec)
end
end_of_workday(day) click to toggle source

Gives the time at the end of the workday, assuming that this time falls on a workday. Note: It pretends that this day is a workday whether or not it really is a workday.

# File lib/business_time/time_extensions.rb, line 23
def end_of_workday(day)
  end_of_workday = BusinessTime::Config.end_of_workday(day)
  change_business_time(day,end_of_workday.hour,end_of_workday.min,end_of_workday.sec)
end
first_business_day(time, options={}) click to toggle source

Returns the time parameter itself if it is a business day or else returns the next business day

# File lib/business_time/time_extensions.rb, line 78
def first_business_day(time, options={})
  while !time.workday?(options)
    time = time + 1.day
  end

  time
end
previous_business_day(time, options={}) click to toggle source

Returns the time parameter itself if it is a business day or else returns the previous business day

# File lib/business_time/time_extensions.rb, line 106
def previous_business_day(time, options={})
  while !time.workday?(options)
    time = time - 1.day
  end

  time
end
roll_backward(time, options={}) click to toggle source

Rolls backwards to the previous end_of_workday when the time is outside of business hours

# File lib/business_time/time_extensions.rb, line 88
def roll_backward(time, options={})
  prev_business_time = if (Time.before_business_hours?(time) || !time.workday?(options))
                         Time.end_of_workday(time - 1.day)
                       elsif Time.after_business_hours?(time)
                         Time.end_of_workday(time)
                       else
                         time.clone
                       end

  while !prev_business_time.workday?(options)
    prev_business_time = Time.end_of_workday(prev_business_time - 1.day)
  end

  prev_business_time
end
roll_forward(time, options={}) click to toggle source

Rolls forward to the next beginning_of_workday when the time is outside of business hours

# File lib/business_time/time_extensions.rb, line 60
def roll_forward(time, options={})
  if Time.before_business_hours?(time) || !time.workday?(options)
    next_business_time = Time.beginning_of_workday(time)
  elsif Time.after_business_hours?(time) || Time.end_of_workday(time) == time
    next_business_time = Time.beginning_of_workday(time + 1.day)
  else
    next_business_time = time.clone
  end

  while !next_business_time.workday?(options)
    next_business_time = Time.beginning_of_workday(next_business_time + 1.day)
  end

  next_business_time
end
weekday?(day) click to toggle source

True if this time falls on a weekday.

# File lib/business_time/time_extensions.rb, line 45
def weekday?(day)
  ActiveSupport::Deprecation.warn("`Time.weekday?(time)` is deprecated. Please use `time.weekday?`")
  day.weekday?
end
work_hours_total(day, options={}) click to toggle source
# File lib/business_time/time_extensions.rb, line 114
def work_hours_total(day, options={})
  return 0 unless day.workday?(options)

  day = day.strftime('%a').downcase.to_sym

  if hours = BusinessTime::Config.work_hours[day]
    BusinessTime::Config.work_hours_total[day] ||= begin
      hours_last = hours.last
      if hours_last == ParsedTime.new(0, 0)
        (ParsedTime.new(23, 59) - hours.first) + 1.minute
      else
        hours_last - hours.first
      end
    end
  else
    BusinessTime::Config.work_hours_total[:default] ||= begin
      BusinessTime::Config.end_of_workday - BusinessTime::Config.beginning_of_workday
    end
  end
end
workday?(day, 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.

# File lib/business_time/time_extensions.rb, line 39
def workday?(day, options={})
  ActiveSupport::Deprecation.warn("`Time.workday?(time)` is deprecated. Please use `time.workday?`")
  day.workday?(options)
end

Private Instance Methods

change_business_time(time, hour, min=0, sec=0) click to toggle source
# File lib/business_time/time_extensions.rb, line 137
def change_business_time time, hour, min=0, sec=0
  time.change(:hour => hour, :min => min, :sec => sec)
end