Module: WorkingHours::Computation
- Included in:
- WorkingHours
- Defined in:
- lib/working_hours/computation.rb
Instance Method Summary collapse
- #add_days(origin, days, config: nil) ⇒ Object
- #add_hours(origin, hours, config: nil) ⇒ Object
- #add_minutes(origin, minutes, config: nil) ⇒ Object
- #add_seconds(origin, seconds, config: nil) ⇒ Object
- #advance_to_closing_time(time, config: nil) ⇒ Object
- #advance_to_working_time(time, config: nil) ⇒ Object
- #in_working_hours?(time, config: nil) ⇒ Boolean
- #next_working_time(time, config: nil) ⇒ Object
- #return_to_exact_working_time(time, config: nil) ⇒ Object
- #return_to_working_time(time, config: nil) ⇒ Object
- #working_day?(time, config: nil) ⇒ Boolean
- #working_days_between(from, to, config: nil) ⇒ Object
- #working_time_between(from, to, config: nil) ⇒ Object
Instance Method Details
#add_days(origin, days, config: nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/working_hours/computation.rb', line 7 def add_days origin, days, config: nil return origin if days.zero? config ||= wh_config time = in_config_zone(origin, config: config) time += (days <=> 0).day until working_day?(time, 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
25 26 27 28 |
# File 'lib/working_hours/computation.rb', line 25 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
30 31 32 33 |
# File 'lib/working_hours/computation.rb', line 30 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
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 68 69 70 71 72 |
# File 'lib/working_hours/computation.rb', line 35 def add_seconds origin, seconds, config: nil config ||= wh_config time = in_config_zone(origin, config: config) 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 working_hours_for(time, config: config).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 working_hours_for(time, config: config).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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/working_hours/computation.rb', line 94 def advance_to_closing_time time, config: nil config ||= wh_config time = in_config_zone(time, config: config) 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 working_hours_for(time, config: config).each do |from, to| return move_time_of_day(time, to) if time_in_day < to end # if none is found, go to next day and loop time = (time + 1.day).beginning_of_day end end |
#advance_to_working_time(time, config: nil) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/working_hours/computation.rb', line 74 def advance_to_working_time time, config: nil config ||= wh_config time = in_config_zone(time, config: config) 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 working_hours_for(time, config: config).each do |from, to| return time if time_in_day >= from and time_in_day < to return move_time_of_day(time, from) 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
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/working_hours/computation.rb', line 151 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 working_hours_for(time, config: config).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
112 113 114 115 |
# File 'lib/working_hours/computation.rb', line 112 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
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/working_hours/computation.rb', line 124 def return_to_exact_working_time time, config: nil config ||= wh_config time = in_config_zone(time, config: config) 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 working_hours_for(time, config: config).reverse_each do |from, to| return time if time_in_day > from and time_in_day <= to return move_time_of_day(time, 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
117 118 119 120 121 122 |
# File 'lib/working_hours/computation.rb', line 117 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
143 144 145 146 147 148 149 |
# File 'lib/working_hours/computation.rb', line 143 def working_day? time, config: nil config ||= wh_config time = in_config_zone(time, config: config) (config[:working_hours][time.wday].present? && !config[:holidays].include?(time.to_date)) || config[:holiday_hours].include?(time.to_date) end |
#working_days_between(from, to, config: nil) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/working_hours/computation.rb', line 162 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
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/working_hours/computation.rb', line 178 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) distance = 0 while from < to from_was = from # look at working ranges time_in_day = from.seconds_since_midnight working_hours_for(from, config: config).each do |begins, ends| if time_in_day >= begins and time_in_day < ends if (to - from) > (ends - time_in_day) # take all the range and continue distance += (ends - time_in_day) from = move_time_of_day(from, ends) else # take only what's needed and stop distance += (to - from) from = to end end end # roll to next business period from = advance_to_working_time(from, config: config) raise "Invalid loop detected in working_time_between (from=#{from.iso8601(12)}, to=#{to.iso8601(12)}, distance=#{distance}, config=#{config}), please open an issue ;)" unless from > from_was end distance.round # round up to supress miliseconds introduced by 24:00 hack end end |