Finding Overlapping Date Intervals in Python when Intervals can Start and End on Seconds or Milliseconds

*爱你&永不变心* 提交于 2019-12-11 12:14:09

问题


What is the best approach to query a list of date intervals for overlapping intervals, given granularity to the second (or even millisecond)?

From the exhaustive list of other questions regarding overlapping integer intervals, I was pointed to the Interval Tree. After reading it, I modified this implementation, which handles integer intervals, for dates.

His search implementation loops from the starting search interval to the ending search interval, recursively appending from the left node or right node as appropriate. This works great if your intervals are close integers (or if your date intervals have granularity down to the hour and are between a day).

I simply decided to loop over the starting search date (converted to seconds) to the ending search interval (converted to seconds via int(time - epoch)), which of course results in a loop of 3600 for searching over a one hour interval. Converting to milliseconds, an hour loop would be 3,600,000!

When searching for overlapping date intervals with granularity down to the second, or millisecond, what is the appropriate algorithmic data structure to use? Is there a better way to implement the Interval Tree than looping over every second?

来源:https://stackoverflow.com/questions/28996910/finding-overlapping-date-intervals-in-python-when-intervals-can-start-and-end-on

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