Algorithm: how to check intersections of recurring events definitions?

我是研究僧i 提交于 2019-12-10 04:13:17

问题


The question comes from MS Outlook calendar behavior.

Imagine I have two recurring events (starting from today): "each second Monday" and "every odd date". Is there any way to check intersections and/or find the first intersecting date algorithmically without brute-forcing over each date?

Definitions can be made in CRON's notations or ICal notation. I think it doesn't matter.

Are there any solutions for this in Gregorian calendar?

UPD: It is also interesting to know how MS Outlook Calendar handles these events. I mean how it discovers that ex. today has two events? I guess should be also technique for determination of whether concrete date suits recurring event definition.

Formally it can be described as two function algorithm:

Date:intersection(Event e1, Event e2)
boolean:contains(Date date, Event ev)

Regarding restrictions, we can go from event definition notation. You can see in outlook (or iCal), that you can define not that complex kind of recurring event.


回答1:


Since the period for the 1st recurrence is 14 days, parity (odd or even) of the day will only change when the month ends in an odd number: Jan, leap Feb, Mar, May, Jul, Aug, Oct, Dec. During a month it will not change.




回答2:


If you are only concerned about when events from different recurrences occur on the same day you could:

  • Establish some arbitrary day in the recent past as day 1.
  • Express each recurrence as a 'number-of-days' between events and a start day; if your event occurs every Monday and every Thursday then express this as 2 recurrences with 2 start days.
  • Use appropriate modular arithmetic to find days when the events from different recurrences collide.

Eg if recurrences 'every Monday' and every '5th day' both collide on day 1, they will next collide on the 36th, 71st, 106th, etc days

If you want something more precise you would need to measure not in whole days but in whatever fraction of a day you are interested in.



来源:https://stackoverflow.com/questions/2904087/algorithm-how-to-check-intersections-of-recurring-events-definitions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!