Module: BusinessTime::TimeExtensions::ClassMethods
- Defined in:
- lib/business_time/time_extensions.rb
Instance Method Summary collapse
- #after_business_hours?(time) ⇒ Boolean
- #before_business_hours?(time) ⇒ Boolean
-
#beginning_of_workday(day) ⇒ Object
Gives the time at the beginning of the workday, assuming that this time falls on a workday.
-
#end_of_workday(day) ⇒ Object
Gives the time at the end of the workday, assuming that this time falls on a workday.
-
#roll_backward(time) ⇒ Object
Rolls backwards to the previous end_of_workday when the time is outside of business hours.
-
#roll_forward(time) ⇒ Object
Rolls forward to the next beginning_of_workday when the time is outside of business hours.
-
#weekday?(day) ⇒ Boolean
True if this time falls on a weekday.
- #work_hours_total(day) ⇒ Object
-
#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.
Instance Method Details
#after_business_hours?(time) ⇒ Boolean
42 43 44 |
# File 'lib/business_time/time_extensions.rb', line 42 def after_business_hours?(time) time.to_i > end_of_workday(time).to_i end |
#before_business_hours?(time) ⇒ Boolean
38 39 40 |
# File 'lib/business_time/time_extensions.rb', line 38 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.
21 22 23 24 |
# File 'lib/business_time/time_extensions.rb', line 21 def beginning_of_workday(day) beginning_of_workday = Time.parse(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.
12 13 14 15 |
# File 'lib/business_time/time_extensions.rb', line 12 def end_of_workday(day) end_of_workday = Time.parse(BusinessTime::Config.end_of_workday(day)) change_business_time(day,end_of_workday.hour,end_of_workday.min,end_of_workday.sec) end |
#roll_backward(time) ⇒ Object
Rolls backwards to the previous end_of_workday when the time is outside of business hours
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/business_time/time_extensions.rb', line 67 def roll_backward(time) prev_business_time = if (Time.before_business_hours?(time) || !Time.workday?(time)) Time.end_of_workday(time - 1.day) elsif Time.after_business_hours?(time) Time.end_of_workday(time) else time.clone end while !Time.workday?(prev_business_time) 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
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/business_time/time_extensions.rb', line 48 def roll_forward(time) if Time.before_business_hours?(time) || !Time.workday?(time) 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 !Time.workday?(next_business_time) 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.
34 35 36 |
# File 'lib/business_time/time_extensions.rb', line 34 def weekday?(day) BusinessTime::Config.weekdays.include? day.wday end |
#work_hours_total(day) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/business_time/time_extensions.rb', line 83 def work_hours_total(day) return 0 unless Time.workday?(day) 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 == '00:00' (Time.parse('23:59') - Time.parse(hours.first)) + 1.minute else Time.parse(hours_last) - Time.parse(hours.first) end end else BusinessTime::Config.work_hours_total[:default] ||= begin Time.parse(BusinessTime::Config.end_of_workday) - Time.parse(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.
28 29 30 31 |
# File 'lib/business_time/time_extensions.rb', line 28 def workday?(day) Time.weekday?(day) && !BusinessTime::Config.holidays.include?(day.to_date) end |