我目前正在一个网站上跟踪项目。在其中,可以创建服务水平协议(SLA)。这些可以配置项目可以处理的星期几,以及每个日期的时间跨度。例如。周一可能是08:00至16:00,周五10:00至14:00。它们还根据优先级配置了截止时间。例如。以“低”优先级创建的项目的截止时间为两周,“优先”项目的截止日期为四小时。
我遇到的问题是在前面描述的时间内计算截止日期。假设我在星期一14:00以“高”优先级创建项目。这意味着这个项目有四个小时。但由于工作时间的原因,我星期一有两个小时(直到16:00),周五又有两个小时。这意味着截止日期必须设置为星期五的12:00。
我花了很长时间在谷歌搜索这个,我可以找到很多例子,找出在给定的开始结束日期之间有多少个工作小时。我只是无法弄清楚如何将其转换为FINDING结束日期时间,给定一个开始时间和一段时间直到截止日期。
day / timespans以以下格式存储在sql数据库中:
日(例如星期一为1)StartHour EndHour
StartHour / EndHour保存为DateTimes,但当然只有时间部分很重要。
我想的方式是,我必须以某种方式迭代这些时间并进行一些日期时间计算。我只是无法弄清楚那些计算应该是什么,最好的方法是什么。
在我写这篇文章时,我在网站上找到了this Question。这是我想要的,我现在正在玩它,但我仍然迷失在如何让它在我的动态工作日/小时内工作。
答案 0 :(得分:2)
这里有一些可能有用的C#代码,它可能更清晰,但它是一个快速的初稿。
class Program
{
static void Main(string[] args)
{
// Test
DateTime deadline = DeadlineManager.CalculateDeadline(DateTime.Now, new TimeSpan(4, 0, 0));
Console.WriteLine(deadline);
Console.ReadLine();
}
}
static class DeadlineManager
{
public static DateTime CalculateDeadline(DateTime start, TimeSpan workhours)
{
DateTime current = new DateTime(start.Year, start.Month, start.Day, start.Hour, start.Minute, 0);
while(workhours.TotalMinutes > 0)
{
DayOfWeek dayOfWeek = current.DayOfWeek;
Workday workday = Workday.GetWorkday(dayOfWeek);
if(workday == null)
{
DayOfWeek original = dayOfWeek;
while (workday == null)
{
current = current.AddDays(1);
dayOfWeek = current.DayOfWeek;
workday = Workday.GetWorkday(dayOfWeek);
if (dayOfWeek == original)
{
throw new InvalidOperationException("no work days");
}
}
current = current.AddHours(workday.startTime.Hour - current.Hour);
current = current.AddMinutes(workday.startTime.Minute - current.Minute);
}
TimeSpan worked = Workday.WorkHours(workday, current);
if (workhours > worked)
{
workhours = workhours - worked;
// Add one day and reset hour/minutes
current = current.Add(new TimeSpan(1, current.Hour * -1, current.Minute * -1, 0));
}
else
{
current.Add(workhours);
return current;
}
}
return DateTime.MinValue;
}
}
class Workday
{
private static readonly Dictionary<DayOfWeek, Workday> Workdays = new Dictionary<DayOfWeek, Workday>(7);
static Workday()
{
Workdays.Add(DayOfWeek.Monday, new Workday(DayOfWeek.Monday, new DateTime(1, 1, 1, 10, 0, 0), new DateTime(1, 1, 1, 16, 0, 0)));
Workdays.Add(DayOfWeek.Tuesday, new Workday(DayOfWeek.Tuesday, new DateTime(1, 1, 1, 10, 0, 0), new DateTime(1, 1, 1, 16, 0, 0)));
Workdays.Add(DayOfWeek.Wednesday, new Workday(DayOfWeek.Wednesday, new DateTime(1, 1, 1, 10, 0, 0), new DateTime(1, 1, 1, 16, 0, 0)));
Workdays.Add(DayOfWeek.Thursday, new Workday(DayOfWeek.Thursday, new DateTime(1, 1, 1, 10, 0, 0), new DateTime(1, 1, 1, 16, 0, 0)));
Workdays.Add(DayOfWeek.Friday, new Workday(DayOfWeek.Friday, new DateTime(1, 1, 1, 10, 0, 0), new DateTime(1, 1, 1, 14, 0, 0)));
}
public static Workday GetWorkday(DayOfWeek dayofWeek)
{
if (Workdays.ContainsKey(dayofWeek))
{
return Workdays[dayofWeek];
}
else return null;
}
public static TimeSpan WorkHours(Workday workday, DateTime time)
{
DateTime sTime = new DateTime(time.Year, time.Month, time.Day,
workday.startTime.Hour, workday.startTime.Millisecond, workday.startTime.Second);
DateTime eTime = new DateTime(time.Year, time.Month, time.Day,
workday.endTime.Hour, workday.endTime.Millisecond, workday.endTime.Second);
if (sTime < time)
{
sTime = time;
}
TimeSpan span = eTime - sTime;
return span;
}
public static DayOfWeek GetNextWeekday(DayOfWeek dayOfWeek)
{
int i = (dayOfWeek == DayOfWeek.Saturday) ? 0 : ((int)dayOfWeek) + 1;
return (DayOfWeek)i;
}
private Workday(DayOfWeek dayOfWeek, DateTime start, DateTime end)
{
this.dayOfWeek = dayOfWeek;
this.startTime = start;
this.endTime = end;
}
public DayOfWeek dayOfWeek;
public DateTime startTime;
public DateTime endTime;
}
答案 1 :(得分:1)
有一个可以起作用的递归解决方案,试着按照以下思路思考:
public DateTime getDeadline(SubmitTime, ProjectTimeAllowed)
{
if (SubmitTime+ProjectTimeAllowed >= DayEndTime)
return getDeadline(NextDayStart, ProjectTimeAllowed-DayEndTime-SubmitTime)
else
return SubmitTime + ProjectTimeAllowed
}
显然这是非常粗糙的伪代码。希望它只是给你另一种思考问题的方法。
答案 2 :(得分:1)
这是我将如何做到的。该算法是为了查看问题是否可以在今天关闭,如果没有,请使用今天的所有时间来减少问题的剩余时间并转到明天。
答案 3 :(得分:1)
使用Stu的answer作为起点,修改IsInBusinessHours函数以查找date参数的营业时间。可以使用如下过程:
CREATE PROCEDURE [dbo].[IsInBusinessHours]
@MyDate DateTime
AS
BEGIN
SELECT CASE Count(*) WHEN 0 THEN 0 ELSE 1 END AS IsBusinessHour
FROM WorkHours
WHERE (DATEPART(hour, StartHours) <= DATEPART(hour, @MyDate)) AND (DATEPART(hour, EndHours) > DATEPART(hour, @MyDate)) AND (Day = DATEPART(WEEKDAY,
@MyDate))
END