我有一个处理日期时间段的问题,当一天被划分为从不同时间开始的班次时。我希望在 C# 中解决这个问题
示例 1:我有一个从 20150901 06:00am 到 20150902 03:00am 的事件。我的工作班次是 D(06:00 到 18:00)和 N(18:00 到 06:00)。我想将事件分配给班次日期时间值。
START DATETIME END DATETIME
20150901 0600 20150902 0300
SHIFT START END
D 0600 1800
N 1800 0600
我应该得到的时间段是:
DATE SHIFT STARTTIME ENDTIME
20150901 D 0600 1800
20150901 N 1800 0300
示例 2:一天分为四班,但一天从早上 06:00 开始(01:0600-1200、02:1200-1800、03:1800-2400、04:0000-0600)。活动从 20150901 08:00am 到 20150903 10:00am
START DATETIME END DATETIME
20150901 0800 20150903 1000
SHIFT START END
01 0600 1159
02 1200 1759
03 1800 2359
04 0000 0559
我应该得到的时间段是:
DATE SHIFT STARTTIME ENDTIME
20150901 01 0800 1200
20150901 02 1200 1800
20150901 03 1800 0000
20150901 04 0000 0600
20150902 01 0600 1200
20150902 02 1200 1800
20150902 03 1800 0000
20150902 04 0000 0600
20150903 01 0600 1000
示例 3:一天分为四班,但一天从早上 06:00 开始(01:0600-1200、02:1200-1800、03:1800-2400、04:0000-0600)。活动从 20150901 02:00am 到 20150902 10:00am
START DATETIME END DATETIME
20150901 0200 20150902 1000
SHIFT START END
01 0600 1159
02 1200 1759
03 1800 2359
04 0000 0559
我应该得到的时间段是:
DATE SHIFT STARTTIME ENDTIME
20150831 04 0200 0600
20150901 01 0800 1200
20150901 02 1200 1800
20150901 03 1800 0000
20150901 04 0000 0600
20150902 01 0600 1000
编辑1:添加我拥有的源代码。
编辑 2:添加示例 3 并更正 Matt Johnson 提出的一些观点
该代码适用于某些情况,但是当事件以某种方式超出轮班边界时我仍然遇到错误(例如,事件在第一次轮班之前开始)。
public class slot
{
public DateTime date;
public TimeSpan startHour;
public TimeSpan endHour;
public string shiftCode;
}
public List<slot> getSlots()
{
slot[] shifts = new slot[4];
shifts[0] = new slot();
shifts[0].startHour = new TimeSpan(06, 00, 00);
shifts[0].endHour = new TimeSpan(12, 00, 00);
shifts[0].shiftCode = "01";
shifts[1] = new slot();
shifts[1].startHour = new TimeSpan(12, 00, 00);
shifts[1].endHour = new TimeSpan(18, 00, 00);
shifts[1].shiftCode = "02";
shifts[2] = new slot();
shifts[2].startHour = new TimeSpan(18, 00, 00);
shifts[2].endHour = new TimeSpan(00, 00, 00);
shifts[2].shiftCode = "03";
shifts[3] = new slot();
shifts[3].startHour = new TimeSpan(00, 00, 00);
shifts[3].endHour = new TimeSpan(06, 00, 00);
shifts[3].shiftCode = "04";
DateTime startEvent = new DateTime(2015, 09, 01, 08, 00, 00);
DateTime endEvent = new DateTime(2015, 09, 03, 10, 00, 00);
int i = 0;
//To find the starting shift for the event
while (!(
shifts[i].startHour <= shifts[i].endHour
? (startEvent.TimeOfDay >= shifts[i].startHour && startEvent.TimeOfDay < shifts[i].endHour)
: (startEvent.TimeOfDay >= shifts[i].startHour || startEvent.TimeOfDay < shifts[i].endHour)
))
i++;
DateTime slotStart = startEvent;
DateTime slotEnd = slotStart.Date + shifts[i].endHour;
List<slot> slotList = new List<slot>();
while(endEvent >= slotEnd)
{
slot newSlot = new slot();
newSlot.date = slotStart;
newSlot.startHour = slotStart.TimeOfDay;
newSlot.endHour = shifts[i].endHour;
newSlot.shiftCode = shifts[i].shiftCode;
slotList.Add(newSlot);
i++;
if (i >= shifts.Length)
i = 0;
if (shifts[i].startHour < newSlot.endHour)
slotStart = slotStart.Date.AddDays(1);
slotEnd = slotStart.Date + shifts[i].endHour;
slotStart = slotStart.Date + shifts[i].startHour;
}
slot lastSlot = new slot();
lastSlot.date = slotStart;
lastSlot.startHour = slotStart.TimeOfDay;
lastSlot.endHour = endEvent.TimeOfDay;
lastSlot.shiftCode = shifts[i].shiftCode;
slotList.Add(lastSlot);
return slotList;
}