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

Returns:

  • (Boolean)


50
51
52
# File 'lib/business_time/time_extensions.rb', line 50

def after_business_hours?(time)
  time.to_i > end_of_workday(time).to_i
end

#before_business_hours?(time) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/business_time/time_extensions.rb', line 46

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.



28
29
30
31
# File 'lib/business_time/time_extensions.rb', line 28

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.



19
20
21
22
# File 'lib/business_time/time_extensions.rb', line 19

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) ⇒ Object

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



75
76
77
78
79
80
81
# File 'lib/business_time/time_extensions.rb', line 75

def first_business_day(time)
  while !time.workday?
    time = time + 1.day
  end

  time
end

#previous_business_day(time) ⇒ Object

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



103
104
105
106
107
108
109
# File 'lib/business_time/time_extensions.rb', line 103

def previous_business_day(time)
  while !time.workday?
    time = time - 1.day
  end

  time
end

#roll_backward(time) ⇒ Object

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/business_time/time_extensions.rb', line 85

def roll_backward(time)
  prev_business_time = if (Time.before_business_hours?(time) || !time.workday?)
                         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?
    prev_business_time = Time.end_of_workday(prev_business_time - 1.day)
  end

  prev_business_time
end

#roll_forward(time) ⇒ Object

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/business_time/time_extensions.rb', line 56

def roll_forward(time)

  if Time.before_business_hours?(time) || !time.workday?
    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?
    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.

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/business_time/time_extensions.rb', line 41

def weekday?(day)
  ActiveSupport::Deprecation.warn("`Time.weekday?(time)` is deprecated. Please use `time.weekday?`")
  day.weekday?
end

#work_hours_total(day) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/business_time/time_extensions.rb', line 111

def work_hours_total(day)
  return 0 unless day.workday?

  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) ⇒ 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)


35
36
37
38
# File 'lib/business_time/time_extensions.rb', line 35

def workday?(day)
  ActiveSupport::Deprecation.warn("`Time.workday?(time)` is deprecated. Please use `time.workday?`")
  day.workday?
end