Module: WorkingHours::Computation

Included in:
WorkingHours
Defined in:
lib/working_hours/computation.rb

Instance Method Summary collapse

Instance Method Details

#add_days(origin, days, config: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/working_hours/computation.rb', line 7

def add_days origin, days, config: nil
  config ||= wh_config
  time = in_config_zone(origin, config: config)
  while days > 0
    time += 1.day
    days -= 1 if working_day?(time, config: config)
  end
  while days < 0
    time -= 1.day
    days += 1 if working_day?(time, config: config)
  end
  convert_to_original_format time, origin
end

#add_hours(origin, hours, config: nil) ⇒ Object



21
22
23
24
# File 'lib/working_hours/computation.rb', line 21

def add_hours origin, hours, config: nil
  config ||= wh_config
  add_minutes origin, hours * 60, config: config
end

#add_minutes(origin, minutes, config: nil) ⇒ Object



26
27
28
29
# File 'lib/working_hours/computation.rb', line 26

def add_minutes origin, minutes, config: nil
  config ||= wh_config
  add_seconds origin, minutes * 60, config: config
end

#add_seconds(origin, seconds, config: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/working_hours/computation.rb', line 31

def add_seconds origin, seconds, config: nil
  config ||= wh_config
  time = in_config_zone(origin, config: config).round
  while seconds > 0
    # roll to next business period
    time = advance_to_working_time(time, config: config)
    # look at working ranges
    time_in_day = time.seconds_since_midnight
    config[:working_hours][time.wday].each do |from, to|
      if time_in_day >= from and time_in_day < to
        # take all we can
        take = [to - time_in_day, seconds].min
        # advance time
        time += take
        # decrease seconds
        seconds -= take
      end
    end
  end
  while seconds < 0
    # roll to previous business period
    time = return_to_exact_working_time(time, config: config)
    # look at working ranges
    time_in_day = time.seconds_since_midnight
    config[:working_hours][time.wday].reverse_each do |from, to|
      if time_in_day > from and time_in_day <= to
        # take all we can
        take = [time_in_day - from, -seconds].min
        # advance time
        time -= take
        # decrease seconds
        seconds += take
      end
    end
  end
  convert_to_original_format(time.round, origin)
end

#advance_to_closing_time(time, config: nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/working_hours/computation.rb', line 88

def advance_to_closing_time time, config: nil
  config ||= wh_config
  time = in_config_zone(time, config: config).round
  loop do
    # skip holidays and weekends
    while not working_day?(time, config: config)
      time = (time + 1.day).beginning_of_day
    end
    # find next working range after time
    time_in_day = time.seconds_since_midnight
    time = time.beginning_of_day
    (config[:working_hours][time.wday] || {}).each do |from, to|
      return time + to if time_in_day >= from and time_in_day < to
      return time + to if from >= time_in_day
    end
    # if none is found, go to next day and loop
    time = time + 1.day
  end
end

#advance_to_working_time(time, config: nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/working_hours/computation.rb', line 69

def advance_to_working_time time, config: nil
  config ||= wh_config
  time = in_config_zone(time, config: config).round
  loop do
    # skip holidays and weekends
    while not working_day?(time, config: config)
      time = (time + 1.day).beginning_of_day
    end
    # find first working range after time
    time_in_day = time.seconds_since_midnight
    (config[:working_hours][time.wday] || {}).each do |from, to|
      return time if time_in_day >= from and time_in_day < to
      return time + (from - time_in_day) if from >= time_in_day
    end
    # if none is found, go to next day and loop
    time = (time + 1.day).beginning_of_day
  end
end

#in_working_hours?(time, config: nil) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
152
153
154
155
# File 'lib/working_hours/computation.rb', line 146

def in_working_hours? time, config: nil
  config ||= wh_config
  time = in_config_zone(time, config: config)
  return false if not working_day?(time, config: config)
  time_in_day = time.seconds_since_midnight
  config[:working_hours][time.wday].each do |from, to|
    return true if time_in_day >= from and time_in_day < to
  end
  false
end

#next_working_time(time, config: nil) ⇒ Object



108
109
110
111
# File 'lib/working_hours/computation.rb', line 108

def next_working_time(time, config: nil)
  time = advance_to_closing_time(time, config: config) if in_working_hours?(time, config: config)
  advance_to_working_time(time, config: config)
end

#return_to_exact_working_time(time, config: nil) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/working_hours/computation.rb', line 120

def return_to_exact_working_time time, config: nil
  config ||= wh_config
  time = in_config_zone(time, config: config).round
  loop do
    # skip holidays and weekends
    while not working_day?(time, config: config)
      time = (time - 1.day).end_of_day
    end
    # find last working range before time
    time_in_day = time.seconds_since_midnight
    (config[:working_hours][time.wday] || {}).reverse_each do |from, to|
      # round is used to suppress miliseconds hack from `end_of_day`
      return time if time_in_day > from and time_in_day <= to
      return (time - (time_in_day - to)) if to <= time_in_day
    end
    # if none is found, go to previous day and loop
    time = (time - 1.day).end_of_day
  end
end

#return_to_working_time(time, config: nil) ⇒ Object



113
114
115
116
117
118
# File 'lib/working_hours/computation.rb', line 113

def return_to_working_time(time, config: nil)
  # return_to_exact_working_time may return values with a high number of milliseconds,
  # this is necessary for the end of day hack, here we return a rounded value for the
  # public API
  return_to_exact_working_time(time, config: config).round
end

#working_day?(time, config: nil) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
143
144
# File 'lib/working_hours/computation.rb', line 140

def working_day? time, config: nil
  config ||= wh_config
  time = in_config_zone(time, config: config)
  config[:working_hours][time.wday].present? and not config[:holidays].include?(time.to_date)
end

#working_days_between(from, to, config: nil) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/working_hours/computation.rb', line 157

def working_days_between from, to, config: nil
  config ||= wh_config
  if to < from
    -working_days_between(to, from, config: config)
  else
    from = in_config_zone(from, config: config)
    to = in_config_zone(to, config: config)
    days = 0
    while from.to_date < to.to_date
      from += 1.day
      days += 1 if working_day?(from, config: config)
    end
    days
  end
end

#working_time_between(from, to, config: nil) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/working_hours/computation.rb', line 173

def working_time_between from, to, config: nil
  config ||= wh_config
  if to < from
    -working_time_between(to, from, config: config)
  else
    from = advance_to_working_time(in_config_zone(from, config: config))
    to = in_config_zone(to, config: config).round
    distance = 0
    while from < to
      # look at working ranges
      time_in_day = from.seconds_since_midnight
      config[:working_hours][from.wday].each do |begins, ends|
        if time_in_day >= begins and time_in_day < ends
          # take all we can
          take = [ends - time_in_day, to - from].min
          # advance time
          from += take
          # increase counter
          distance += take
        end
      end
      # roll to next business period
      from = advance_to_working_time(from, config: config)
    end
    distance.round # round up to supress miliseconds introduced by 24:00 hack
  end
end