1

I need to count amount of time in specified range. For example, I have range (let's call it peak hours) 12:00-14:00. And i have another range(visit time), that might change, for ex 9:00-15:00. How do I get intersected hours for these 2 ranges?

As result I would like to get something like: {peak_hours: 2, regular_hours: 4} Here peak_hours value is 2 as that many peak hours overlap with regular hours. And, regular_hours value is 4 as that many regular hours do not overlap with peak hours.

I'm kinda stuck with solution. I tried to use time ranges, but that didn't work for me. This is the code

peak_hours_range = Time.parse(peak_hour_start)..Time.parse(peak_hour_end)

session_range = visit_start..visit_end

inters = session_range.to_a & peak_hours_range.to_a

But this throws me type error

Wand Maker
  • 18,476
  • 8
  • 53
  • 87
Avdept
  • 2,261
  • 2
  • 26
  • 48
  • I don't understand what peak and regular hours are. – xlembouras Feb 02 '16 at 17:58
  • thats just 2 different ranges. In the end - i want to know how many regular hours intersects with peak hours – Avdept Feb 02 '16 at 18:03
  • @Jordan, I don't doubt that this is a dup, but not a dup of the referenced question, which deals with overlapping dates. – Cary Swoveland Feb 02 '16 at 18:07
  • @CarySwoveland I think at least one of the answer to the referenced question solves OP's problem, but nevertheless I've reopened this one. – Jordan Running Feb 02 '16 at 18:08
  • Question is not clear - what you mean by intersected hours? Can you explain your expected output? – Wand Maker Feb 02 '16 at 18:18
  • @WandMaker By intersected hours - i mean intersected ranges of time. And as result i would like to get amount of hours from range(visit time) that intersects with range(peak hours) – Avdept Feb 02 '16 at 18:28
  • How did you get 4 hours of intersection in regular hours for peak hours of 2? If peak hours were 8:00-10:00, what would be output? – Wand Maker Feb 02 '16 at 18:34
  • @WandMaker with your peak hours output would be: {peak_hours: 1, regular_hours: 5} – Avdept Feb 02 '16 at 18:38
  • What does 1 for peak hour mean? Is it because 1 hour of peak times is out of regular hour range or inside the range? Similarly, why regular hours value is 5? – Wand Maker Feb 02 '16 at 18:41
  • 1 peak hour because from range 9:00-15:00 only 1 hour inside range of 8:00 - 10:00. 5 regular hours - thats just `total hours - peak hours` – Avdept Feb 02 '16 at 18:58

2 Answers2

1

You can always try to find the intersection yourself.

inters = nil

intersection_min = [peak_hour_start, visit_start].max
intersection_max = [peak_hour_end, visit_end].min

if intersection_min < intersection_max
  inters = [intersection_min, intersection_max]
end

inters

Of course this can be cleaned up by extracting it out into it's own method.

T J
  • 1,312
  • 7
  • 7
1

Here is one way to do it, we find the total hours in both ranges included, and then remove the peak hours from it to get effective regular hours.

require "time"

peak_hour_start = "12:00"
peak_hour_end = "14:00"

regular_hour_start = "9:00"
regular_hour_end = "15:00"

ph = (Time.parse(peak_hour_start).hour...Time.parse(peak_hour_end).hour).to_a
#=> [12, 13]
rh = (Time.parse(regular_hour_start).hour...Time.parse(regular_hour_end).hour).to_a
#=> [9, 10, 11, 12, 13, 14]
total = (ph + rh).uniq
#=> [12, 13, 9, 10, 11, 14]
r = {peak_hours: (ph - rh).size, regular_hours: (total - ph).size}
#=> {:peak_hours=>2, :regular_hours=>4}
Wand Maker
  • 18,476
  • 8
  • 53
  • 87