gpt4 book ai didi

c# - 在这个算法中我缺少什么来找到两个可能跨越不同天数的 TimeSpans 之间的 TimeOfDay?

转载 作者:太空狗 更新时间:2023-10-29 21:54:46 26 4
gpt4 key购买 nike

我有一个 List<T>一天 24 小时内的可用时间,以及两个 TimeSpans , minTime 和 maxTime。

我需要在 List<T> 中找到一天中的某个时间落在 minTime 之间和 maxTime ,但是由于它在多个时区中使用,所以 minTime 和 maxTime 可以在不同的日子,并且跨越大约从下午 1 点到第二天凌晨 1 点

我最接近的是这个,但我觉得我在这里缺少一些主要组件,或者做一些非常低效的事情,因为我是 TimeSpan 的新手。目的。我就是想不通是什么……

// Make new TimeSpan out of maxTime to eliminate any extra days (TotalHours >= 24),
// then check if time on the MaxTime is earlier than the MinTime
if (new TimeSpan(maxTime.Hours, maxTime.Minutes, maxTime.Seconds) < minTime)
{
// If time on MaxTime is earlier than MinTime, the two times span separate days,
// so find first time after minTime OR before maxTime
nextAvailableTime = Times.FirstOrDefault(p =>
(p.Time.TimeOfDay >= minTime || (p.Time.TimeOfDay < maxTime))
&& p.Count < ConcurrentAppointments);
}
else
{
// If time on MaxTime is later than MinTime, the two times are for the same day
// so find first time after minTime AND before maxTime
nextAvailableTime = Times.FirstOrDefault(p =>
(p.Time.TimeOfDay >= minTime && p.Time.TimeOfDay < maxTime)
&& p.Count < ConcurrentAppointments);
}

列表Times使用 EST(我的本地时间),但是 minTimemaxTime可以基于其他时区。

例如,如果我们针对夏威夷时区运行此算法,我们最终会得到 minTime = new TimeSpan(13, 0, 0)maxTime = new TimeSpan(25, 0, 0) ,从早上 8 点到晚上 8 点 HST = 下午 1 点到凌晨 1 点 EST。

Times集合是 List<AppointmentTime> , 和 AppointmentTime是一个看起来像这样的类:

class AppointmentTime
{
DateTime Time { get; set; }
int Count { get; set; }
}

我很确定我在这里遗漏了一些重要的东西,或者应该有一种我不知道的更有效的方法来做到这一点,但我真的想不出它可能是什么。我的算法有问题吗?或者找到一个更有效的方法 TimeOfDay两个之间TimeSpans可能跨越不同的日子?

更新

我根据 CasperOne's answer 弄清楚了我遗漏了什么.我忘记了日期实际上很重要,因为我的时间跨越不同的时区。

使用我上面的夏威夷时区示例,在星期一安排约会会导致在星期天晚上错误地安排夏威夷约会。

我的解决方案是在为 24 小时工作日的“第一个窗口”安排约会之前检查前一天是否有效,并将约会日期调整为 .AddDays(maxTime.Days)maxTime 比较时

// If time on MaxTime is earlier than MinTime, the two times span separate days,
// so find first time after minTime OR before maxTime if previous day has appointments set as well
var isPreviousDayValid = IsValidDate(AppointmentDate.AddDays(-1));

nextAvailableTime = Times.FirstOrDefault(p =>
(p.Time.TimeOfDay >= minTime
|| (p.Time.AddDays(maxTime.Days).TimeOfDay < maxTime && isPreviousDayValid)
) && p.Count < ConcurrentAppointments);

最佳答案

一般的想法是,不要比较时间,比较日期;将您的窗口从时间翻译成日期,剩下的就很容易了。

您可以生成一组新的 DateTime使用 Date property 比较列表中每个 项的实例以比较最小值和最大值作为计算您要比较的范围上限的基础。

这假设您的 minTime 总是小于 maxTime,并且如果 your window covers more than one day您可以通过 Hours property 来表示重叠到新一天的范围TimeSpan 上的值that is greater than 24 hours .

您必须查看前一天和后一天是否有一个窗口。例如:

   (1)  (2)  (3)  (4)  (5)
----x----x----x----x----x----

(1) - 1/1/1900 11:00 PM - Date component in your list - 1 day + min time
(2) - 1/2/1900 12:05 AM - this is the date and time from your list
(3) - 1/2/1900 01:00 AM - Date component in your list - 1 day + max time
(4) - 1/2/1900 11:00 PM - Date component in your list + min time
(5) - 1/3/1900 01:00 AM - Date component in your list + max time

这意味着您需要创建两个窗口并检查您是否位于:

nextAvailableTime = Times.FirstOrDefault(p => {
// Check count first, get this out of the way.
if (!(p.Count < ConcurrentAppointments)) return false;

// The date time and the date component
DateTime dt = p.Time;
DateTime d = dt.Date;

// The windows
DateTime prevWindowMin = d.AddDays(-1) + minTime;
DateTime prevWindowMax = d.AddDays(-1) + maxTime;
DateTime windowMin = d + minTime;
DateTime windowMax = d + maxTime;

// Is it in *either* window;
return
(prevWindowMin <= dt && dt <= prevWindowMax)||
(windowMin <= dt && dt <= windowMax);
});

从您的问题中还不完全清楚,但是如果一天中的时间其他不是您列表中项目的 Date 组件,您可以替换 p.Time 用于 that 日期的 Date 组件(适当减去一天以创建窗口),它应该可以工作。

关于c# - 在这个算法中我缺少什么来找到两个可能跨越不同天数的 TimeSpans 之间的 TimeOfDay?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13885661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com