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