I want my program to reset statistics when new work shift starts. My shift hours are predefined as:
- Day shift 4:00 - 16:25 Mon-Thu (4:00 - 15:55 Fri-Sun)
- Eve shift 16:25 - 4:00 Mon-Thu (15:55 - 4:00 Fri-Sun)
This is what I'm doing at the moment, it works, but I can't figure out how to do a Time comparison when end of shift is in the AM, so right now it works when end of shift is set to 23:59:59, but when I change it to 4AM, it falls on its ass...
DateTime d1 = new DateTime(2020, 01, 01, 16, 25, 00); //Mon-Thur shift change
DateTime d2 = new DateTime(2020, 01, 01, 15, 55, 00); //Fri-Sun shift change
DateTime d3 = new DateTime(2020, 01, 01, 23, 59, 59); //Evening shift end
DateTime d4 = new DateTime(2020, 01, 01, 04, 00, 00); //Morning shift start
DateTime d5 = DateTime.Now;
if (d5.DayOfWeek == DayOfWeek.Monday || d5.DayOfWeek == DayOfWeek.Tuesday || d5.DayOfWeek == DayOfWeek.Wednesday || d5.DayOfWeek == DayOfWeek.Thursday)
{
if (d5.TimeOfDay >= d4.TimeOfDay && d5.TimeOfDay < d1.TimeOfDay)
{
dayShift = true;
eveShift = false;
}
}
if (d5.DayOfWeek == DayOfWeek.Friday || d5.DayOfWeek == DayOfWeek.Saturday || d5.DayOfWeek == DayOfWeek.Sunday)
{
if (d5.TimeOfDay >= d4.TimeOfDay && d5.TimeOfDay < d2.TimeOfDay)
{
dayShift = true;
eveShift = false;
}
}
if (d5.DayOfWeek == DayOfWeek.Monday || d5.DayOfWeek == DayOfWeek.Tuesday || d5.DayOfWeek == DayOfWeek.Wednesday || d5.DayOfWeek == DayOfWeek.Thursday)
{
if (d5.TimeOfDay >= d1.TimeOfDay && d5.TimeOfDay < d3.TimeOfDay)
{
dayShift = false;
eveShift = true;
}
}
if (d5.DayOfWeek == DayOfWeek.Friday || d5.DayOfWeek == DayOfWeek.Saturday || d5.DayOfWeek == DayOfWeek.Sunday)
{
if (d5.TimeOfDay >= d2.TimeOfDay && d5.TimeOfDay < d3.TimeOfDay)
{
dayShift = false;
eveShift = true;
}
}
What could I do to be able to compare the TimeOfDay to AM hours?
I'd like to check if the time is between d1 and d4. Any ideas?
I would change the approach here. rather than a set of times, I'd instead define what constitutes a 'shift' definition in an object, have a list of those objects and then iterate through them and test the current date/time against them...
This ended up being far more complicated than I thought at first glance, because 2:33AM on Tuesday would actually be towards the end of Monday's Evening shift I guess...
But I'd do this:
To use, you can test any date/time and get back either 'Day' or 'Night'
I've only tested briefly so possibly some logic errors in there somewhere, but should be fairly easy to debug....