How to combine overlapping time ranges (time ranges union)

后端 未结 10 1392
渐次进展
渐次进展 2020-12-08 05:19

I have an array with several time ranges inside:

[Tue, 24 May 2011 08:00:00 CEST +02:00..Tue, 24 May 2011 13:00:00 CEST +02:00,
 Tue, 24 May 2011 16:30:00 CE         


        
10条回答
  •  臣服心动
    2020-12-08 05:34

    The Marked answer works well except for few use cases. One of such use case is

    [Tue, 21 June 13:30:00 GMT +0:00..Tue, 21 June 15:30:00 GMT +00:00,
    Tue, 21 June 14:30:00 GMT +0:00..Tue, 21 June 15:30:00 GMT +00:00]
    

    The condition in ranges_overlap does not handle this use case. So I wrote this

    def ranges_overlap?(a, b)
        a.include?(b.begin) || b.include?(a.begin) || a.include?(b.end) || b.include?(a.end)|| (a.begin < b.begin && a.end >= b.end) || (a.begin >= b.begin && a.end < b.end)
    end
    

    This is handling all the edge cases for me so far.

提交回复
热议问题