Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/extensions/time.rb

Overview

Add workday and weekday concepts to the Time class

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.after_business_hours?(time) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/extensions/time.rb', line 42

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

.before_business_hours?(time) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/extensions/time.rb', line 38

def before_business_hours?(time)
  time < beginning_of_workday(time)
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.



19
20
21
22
23
# File 'lib/extensions/time.rb', line 19

def beginning_of_workday(day)
  format = "%B %d %Y #{BusinessTime::Config.beginning_of_workday}"
  Time.zone ? Time.zone.parse(day.strftime(format)) :
      Time.parse(day.strftime(format))
end

.during_business_hours?(time) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/extensions/time.rb', line 46

def during_business_hours?(time)
  workday?(time) && !before_business_hours?(time) && !after_business_hours?(time)
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.



9
10
11
12
13
# File 'lib/extensions/time.rb', line 9

def end_of_workday(day)
  format = "%B %d %Y #{BusinessTime::Config.end_of_workday}"
  Time.zone ? Time.zone.parse(day.strftime(format)) :
      Time.parse(day.strftime(format))
end

.roll_forward(time) ⇒ Object

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/extensions/time.rb', line 52

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)
    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 += 1.day
  end

  next_business_time
end

.weekday?(day) ⇒ Boolean

True if this time falls on a weekday.

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/extensions/time.rb', line 33

def weekday?(day)
  # TODO AS: Internationalize this!
  [1,2,3,4,5].include? day.wday
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)


27
28
29
30
# File 'lib/extensions/time.rb', line 27

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

Instance Method Details

#business_time_left_to(time) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/extensions/time.rb', line 71

def business_time_left_to(time)
  if time.to_date == self.to_date
  end
  time_left = 0
  start_day = self
  while start_day.to_date < time.to_date
    parsed_start_day = Time.parse(start_day.to_s)
    time_left += start_day.business_time_left_to_end
    start_day = Time.beginning_of_workday(parsed_start_day) + 1.day
  end
  time_left += time.business_time_passed_from_beginning
  time_left
end

#business_time_left_to_endObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/extensions/time.rb', line 85

def business_time_left_to_end
  time = Time.parse(self.to_s)
  workday = Time.workday?(time)
  if workday && Time.during_business_hours?(time)
    Time.end_of_workday(time) - self
  elsif Time.before_business_hours?(time) && workday
    Time.end_of_workday(time) - Time.beginning_of_workday(time)
  else
    0
  end
end

#business_time_passed_from_beginningObject



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/extensions/time.rb', line 97

def business_time_passed_from_beginning
  time = Time.parse(self.to_s)
  workday = Time.workday?(time)
  if workday && Time.during_business_hours?(time)
    self - Time.beginning_of_workday(time)
  elsif Time.after_business_hours?(time) && workday
    Time.end_of_workday(time) - Time.beginning_of_workday(time)
  else
    0
  end
end