Algorithm to detect overlapping recurent time peri

2019-08-09 06:06发布

I'm trying to detect event's colision depending of the days recurrency and the timetable.

A version more complex than this issue Algorithm to detect overlapping periods.

My case : The A event is from 8am to 1pm all the tuesday, thurday and saturday, between two dates.

The B event is from 11am to 2pm all the tuesday and thurday, between two dates(differents of event A)

Here, I putted in red the colisions between the two events.

I spent hour trying to write an algorithm to detect if there at least one colision between the two event.

By the way the date of end of an event (tend) can be undeterminate.

Is there an existing algorithm to manage this?

enter image description here

1条回答
Fickle 薄情
2楼-- · 2019-08-09 06:48

Figure out which one ends first. If both A and B are unlimited just pick some random date in the future.

I'm going to assume that your events repeat on a weekly basis (every week there's the same recurring times). If that's not true replace week with the largest repeting basis (month/year/whatever).

Take the last week before the date you picked in the first step. Generate all the events that happen that week (make sure you do all the checks, since one of the events might start repeating in this period. For each event you should have a concrete timestamp for start and end. Now check if these overlap. If at least some of them do then you have a collision. If not then you're good.

查看更多
登录 后发表回答