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 |
# 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 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
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/working_hours/computation.rb', line 92 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 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
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/working_hours/computation.rb', line 73 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 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
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/working_hours/computation.rb', line 150 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
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 142 |
# 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 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
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
144 145 146 147 148 |
# File 'lib/working_hours/computation.rb', line 144 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
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/working_hours/computation.rb', line 161 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
177 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 |
# File 'lib/working_hours/computation.rb', line 177 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 # 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 |