Module: BusinessTime::TimeExtensions::ClassMethods

Included in:
ActiveSupport::TimeWithZone, Time
Defined in:
lib/business_time/time_extensions.rb

Instance Method Summary collapse

Instance Method Details

#after_business_hours?(time) ⇒ Boolean



54
55
56
# 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) ⇒ Boolean



50
51
52
# 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) ⇒ Object

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.



32
33
34
35
# 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) ⇒ Object

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.



23
24
25
26
# 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 = {}) ⇒ Object

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



78
79
80
81
82
83
84
# 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 = {}) ⇒ Object

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



106
107
108
109
110
111
112
# 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 = {}) ⇒ Object

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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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 = {}) ⇒ Object

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Boolean

True if this time falls on a weekday.



45
46
47
48
# 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 = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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 = {}) ⇒ 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.



39
40
41
42
# 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