简体   繁体   English

计算时间范围与时间范围的重叠

[英]Calculate overlaps of time ranges with time range

I try to calculate the amount (in minutes) of overlaps between a given time range and an array of time ranges.我尝试计算给定时间范围和时间范围数组之间的重叠量(以分钟为单位)。

First I order the ranges by start time, and filter them, if they not overlap with the range.首先,我按开始时间对范围进行排序,如果它们不与范围重叠,则过滤它们。

 const range = ["2019-11-06 08:00", "2019-11-06 17:00"]; // 9h const ranges = [ ["2019-11-06 00:00", "2019-11-06 10:00"], ["2019-11-06 22:00", "2019-11-06 24:00"], ["2019-11-06 09:00", "2019-11-06 12:00"] ] function calculateOvelappings(range, ranges) { const start = moment(range[0]) const end = moment(range[1]) let overlaps = 0 // order ranges by start time ranges = ranges.sort((a, b) => moment(a[0]).unix() - moment(b[0]).unix()) for(let i = 0; i < ranges.length; i++) { const rangeStart = moment(ranges[i][0]) const rangeEnd = moment(ranges[i][1]) if (rangeEnd.isBefore(start) || rangeStart.isAfter(end)) { continue; } } return overlaps } console.log(calculateOvelappings(range, ranges))
 <script src="https://cdn.jsdelivr.net/momentjs/2.14.1/moment-with-locales.min.js"></script>

EDIT编辑

The right answer for overlaps would be 4 hours.重叠的正确答案是 4 小时。 The input range starts at 08:00 in intersects with range 1 (from the array) till 10:00 (2 hours).输入范围从 08:00 开始,与范围 1(来自数组)相交,直到 10:00(2 小时)。 Range 2 is irrelevant.范围 2 无关紧要。 Range 3 overlaps with the input range from 09:00 to 12:00, but from 09:00 to 10:00 is already calculated from range 1. Therefor 2 + (3 - 1) = 4 hours.范围 3 与从 09:00 到 12:00 的输入范围重叠,但已经从范围 1 计算了从 09:00 到 10:00。因此 2 + (3 - 1) = 4 小时。

Is there an elegant algorithm, to calculate this?有没有一个优雅的算法来计算这个? Any help would be appreciated, a pseudo code, or even a theoretical answare.任何帮助,伪代码,甚至是理论上的答案都将不胜感激。

Here is a JsBin: https://jsbin.com/noyuhekura/1/edit?js,console这是一个 JsBin: https://jsbin.com/noyuhekura/1/edit?js,console

 const range = ["2019-11-06 08:00", "2019-11-06 17:00"]; // 9h const ranges = [ ["2019-11-06 00:00", "2019-11-06 10:00"], ["2019-11-06 22:00", "2019-11-06 24:00"], ["2019-11-06 09:00", "2019-11-06 12:00"] ] function calculateOvelappings(range, ranges) { const start = moment(range[0]) const end = moment(range[1]) let overlaps = 0 // order ranges by start time ranges = ranges.sort((a, b) => moment(a[0]).unix() - moment(b[0]).unix()) for(let i = 0; i < ranges.length; i++) { const rangeStart = moment(ranges[i][0]) const rangeEnd = moment(ranges[i][1]) if (rangeEnd.isBefore(start) || rangeStart.isAfter(end)) { continue; } overlaps += rangeEnd - rangeStart // Updated } return overlaps } console.log(calculateOvelappings(range, ranges))
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

you haven't updated overlaps at all.你根本没有更新重叠。 You need to add:您需要添加:

overlaps += (rangeEnd - rangeStart)

otherwise it never changes from 0否则它永远不会从 0 改变

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM