I am trying to resolve an issue in regard to an activity status function for a specific condition. The function is trying to check if an signature on the activity falls within a validity period of two dates within a shift.
The activity has a stored start and finish DateTime as follows:
var startDate = activity.Start; // ex. new DateTime(2023, 05, 25, 18, 00, 00, DateTimeKind.Utc)
var finishDate = activity.Finish; // ex. new DateTime(2023, 06, 01, 04, 00, 00, DateTimeKind.Utc)
In this instance, shifts would be between 18:00 - 04:00 each day (i.e. 25/05 18:00 -> 26/05 04:00, 26/05 18:00 -> 27/05 04:00 etc) which are stored against a shift integer based on the working day (this will usually be in the range of 1-7). If the time that the signature is made is within the validity period then it will go to a "Live" status, otherwise it will go to a "Awaiting Start" status.
Currently the logic in place is as follows. This will return Live
if now
is after midnight within the validity period of a shift but if the signature time is before midnight, then it will return Awaiting Start
.
public void SetStatus (T activity)
{
//....
if (startTime.Date <= DateTime.UtcNow.Date && finishTime.Date >= DateTime.UtcNow.Date)
{
if (finishTime.TimeOfDay < startTime.TimeOfDay)
activity.Status = SetStatusIfShiftCrossesMidnight(startTime, finishTime);
// ... other checks
}
}
private string SetStatusIfShiftCrossesMidnight(DateTime startDate, DateTime finishDate)
{
var now = DateTime.UtcNow;
var start = new DateTime(now.Year, now.Month, now.Day - 1 , startDate.Hour, startDate.Minute,
startDate.Second, DateTimeKind.Utc);
var finish = new DateTime(now.Year, now.Month, now.Day, finishDate.Hour, finishDate.Minute,
finishDate.Second, DateTimeKind.Utc);
if (start <= now && finish > now)
return "Live";
return "Awaiting Start";
}
Any recommendedations on the best way to tackle this?