问题 - 我有一个包含两个日期时间列的列表,我想在其中找到日期重叠的两列之间的时间差距,但各个日期和时间之间存在差距。
我一直在尝试使用 TimePeriod 库,但它无法辨别重叠时段内的实际差距。
日期范围示例。开始日期范围是 3/1/2024。结束日期范围是 6/1/2024 4/1/2024 和 5/1/2024 第 1 行和第 5 行之间存在样本差距。从 4/1 到 5/1 缺少 1 AM 到 5 AM 之间的时间。
var organizeCurvePoints = new List<CurveDateOrganizer>();
foreach (var d in AllCurvePointsInRangeCopy)
{
organizeCurvePoints.Add(new CurveDateOrganizer( d.LocalEffectiveTime, d.LocalTerminationTime));
}
organizeCurvePoints.OrderBy(t => t.EffectiveTime);
TimePeriodCollection effectiveHours = new TimePeriodCollection();
effectiveHours.Add(new TimeRange(startDate, endDate));
// Range of Time in PeriodorganizeCurvePoints
ITimePeriodCollection effectivePeriod = new TimePeriodCombiner<TimeRange>().CombinePeriods(effectiveHours);
// Define All Records in Period
TimePeriodCollection curvePeriodDates = new TimePeriodCollection();
foreach (var day in AllCurvePointsInRangeCopy)
{
curvePeriodDates.Add(new TimeRange(day.LocalEffectiveTime, day.LocalTerminationTime));
}
ITimePeriodCollection changePeriods = new TimePeriodCombiner<TimeRange>().CombinePeriods(organizeCurvePoints);
TimePeriodCollection gaps = new TimePeriodCollection();
foreach (ITimePeriod basePeriod in effectiveHours)
{
gaps.AddAll(new TimeGapCalculator<TimeRange>().GetGaps(changePeriods, basePeriod));
}
// Class to Hold Dates
[Serializable]
[DebuggerDisplay("EffectiveTime {EffectiveTime} TerminationTime {TerminationTime}", Name = "{DateOrganizer}")]
public class CurveDateOrganizer
{
private DateTime _effectiveTime;
public DateTime EffectiveTime
{
get => _effectiveTime;
set
{
if (_effectiveTime != value)
{
_effectiveTime = value;
}
}
}
private DateTime _terminationTime;
public DateTime TerminationTime
{
get => _terminationTime;
set
{
if (_terminationTime != value)
{
_terminationTime = value;
}
}
}
public CurveDateOrganizer(DateTime localEffectiveTime, DateTime localTerminationTime)
{
_effectiveTime = localEffectiveTime;
_terminationTime = localTerminationTime;
}
}
假设这些都在同一时区(例如,UTC),您可能会发现将日期转换为数字(例如,ToUnixTimeSeconds())然后使用类似于下面的代码来查找间隙或重叠会更容易。
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// Array of number ranges (min & max or start & end)
long[,] ranges = { { 1, 5 }, { 3, 7 }, { 10, 15 }, { 12, 20 } };
Console.WriteLine("Gaps in the number ranges:");
foreach (long gap in FindGapsInRanges(ranges))
{
Console.WriteLine(gap);
}
Console.WriteLine("Overlaps in the number ranges:");
foreach (long overlap in FindOverlapsInRanges(ranges))
{
Console.WriteLine(overlap);
}
}
public static IEnumerable<long> FindGapsInRanges(long[,] ranges)
{
long numRanges = ranges.GetLongLength(0);
for (long i = 0; i < numRanges - 1; i++)
{
long rangeEnd = ranges[i, 1];
long nextRangeStart = ranges[i + 1, 0];
if (rangeEnd < nextRangeStart)
{
for (long gap = rangeEnd + 1; gap < nextRangeStart; gap++)
{
yield return gap;
}
}
}
}
public static IEnumerable<long> FindOverlapsInRanges(long[,] ranges)
{
long numRanges = ranges.GetLongLength(0);
for (long i = 0; i < numRanges - 1; i++)
{
long rangeEnd = ranges[i, 1];
long nextRangeStart = ranges[i + 1, 0];
if (rangeEnd >= nextRangeStart)
{
for (long overlap = nextRangeStart; overlap <= rangeEnd; overlap++)
{
yield return overlap;
}
}
}
}
}
运行上面的代码返回以下结果:
Gaps in the number ranges:
8
9
Overlaps in the number ranges:
3
4
5
12
13
14
15