Для расчета смены вы можете использовать этот код
class ShiftInfo : IEquatable<ShiftInfo>
{
public ShiftInfo(TimeSpan startTime, string name)
{
this.StartTime = startTime;
this.Name = name;
}
public TimeSpan StartTime
{
get;
private set;
}
public string Name
{
get;
private set;
}
public bool Equals(ShiftInfo other)
{
return StartTime.Equals(other.StartTime);
}
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
if (obj is ShiftInfo)
{
return this.Equals((ShiftInfo)obj);
}
return false;
}
public override int GetHashCode()
{
return StartTime.GetHashCode();
}
}
class ShiftConfig : IEnumerable<ShiftInfo>
{
private readonly ICollection<ShiftInfo> _shiftInfos;
public ShiftConfig(params ShiftInfo[] shiftInfos)
{
_shiftInfos = shiftInfos.Distinct().OrderBy(e => e.StartTime).ToList();
}
public ShiftConfig(HashSet<ShiftInfo> shiftInfos)
{
_shiftInfos = shiftInfos.OrderBy(e => e.StartTime).ToList();
}
public IEnumerator<ShiftInfo> GetEnumerator()
{
return _shiftInfos.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _shiftInfos.GetEnumerator();
}
}
class ShiftWorkItem
{
public ShiftWorkItem(ShiftInfo shift, DateTime shiftFrom, DateTime shiftUntil, DateTime workFrom, DateTime workUntil)
{
Shift = shift;
ShiftFrom = shiftFrom;
ShiftUntil = shiftUntil;
WorkFrom = workFrom;
WorkUntil = workUntil;
}
public ShiftInfo Shift
{
get;
private set;
}
public DateTime ShiftFrom
{
get;
private set;
}
public DateTime ShiftUntil
{
get;
private set;
}
public TimeSpan ShiftDuration
{
get
{
return ShiftUntil - ShiftFrom;
}
}
public DateTime WorkFrom
{
get;
private set;
}
public DateTime WorkUntil
{
get;
private set;
}
public TimeSpan WorkDuration
{
get
{
return WorkUntil - WorkFrom;
}
}
}
static class ShiftConfigExtensions
{
public static IEnumerable<ShiftWorkItem> EnumerateShifts(this ShiftConfig config, DateTime from, TimeSpan duration)
{
return EnumerateShifts(config, from, from.Add(duration));
}
public static IEnumerable<ShiftWorkItem> EnumerateShifts(this ShiftConfig config, DateTime from, DateTime until)
{
DateTime day = from.Date.AddDays(-1);
DateTime? shiftStart = null;
ShiftInfo lastShift = null;
while (true)
{
foreach (var shift in config)
{
var shiftEnd = day.Add(shift.StartTime);
if (shiftStart != null)
{
if ((shiftStart.Value <= from && shiftEnd >= from) || (shiftStart.Value <= until && shiftEnd >= until) || (shiftStart.Value > from && shiftEnd <= until))
{
var workFrom = shiftStart.Value < from ? from : shiftStart.Value;
var workUntil = shiftEnd > until ? until : shiftEnd;
yield return new ShiftWorkItem(lastShift, shiftStart.Value, shiftEnd, workFrom, workUntil);
}
}
if (shiftEnd >= until)
{
yield break;
}
shiftStart = shiftEnd;
lastShift = shift;
}
day = day.AddDays(1);
}
}
}
Использование
public static void Main(string[] args)
{
var sc = new ShiftConfig(
new ShiftInfo(TimeSpan.FromHours(6), "early"),
new ShiftInfo(TimeSpan.FromHours(14), "late"),
new ShiftInfo(TimeSpan.FromHours(22), "night"));
Console.WriteLine(" | SHIFT | WORK ");
Console.WriteLine(" Date Shiftname | from until dur. | from until dur. ");
Console.WriteLine("======================|=========================|=========================");
foreach (var item in sc.EnumerateShifts(new DateTime(2019, 01, 01, 02, 37, 25), TimeSpan.FromHours(28.34)))
{
Console.WriteLine("{0:yyyy-MM-dd} {1,-10} | {2:HH:mm:ss} {3:HH:mm:ss} {4:0.00}h | {5:HH:mm:ss} {6:HH:mm:ss} {7:0.00}h", item.ShiftFrom.Date, item.Shift.Name, item.ShiftFrom, item.ShiftUntil, item.ShiftDuration.TotalHours, item.WorkFrom, item.WorkUntil, item.WorkDuration.TotalHours);
}
}
, который генерирует
| SHIFT | WORK
Date Shiftname | from until dur. | from until dur.
======================|=========================|=========================
2018-12-31 night | 22:00:00 06:00:00 8.00h | 02:37:25 06:00:00 3.38h
2019-01-01 early | 06:00:00 14:00:00 8.00h | 06:00:00 14:00:00 8.00h
2019-01-01 late | 14:00:00 22:00:00 8.00h | 14:00:00 22:00:00 8.00h
2019-01-01 night | 22:00:00 06:00:00 8.00h | 22:00:00 06:00:00 8.00h
2019-01-02 early | 06:00:00 14:00:00 8.00h | 06:00:00 06:57:49 0.96h
.net fiddle sample
или для покрытия вашего образца
public static void Main(string[] args)
{
var sc = new ShiftConfig(
new ShiftInfo(TimeSpan.FromHours(0), "night"),
new ShiftInfo(TimeSpan.FromHours(8), "early"),
new ShiftInfo(TimeSpan.FromHours(16), "late"));
Console.WriteLine(" | SHIFT | WORK ");
Console.WriteLine(" Date Shiftname | from until dur. | from until dur. ");
Console.WriteLine("======================|=========================|=========================");
foreach (var item in sc.EnumerateShifts(new DateTime(2019, 01, 01, 15, 55, 00), TimeSpan.FromMinutes(20)))
{
Console.WriteLine("{0:yyyy-MM-dd} {1,-10} | {2:HH:mm:ss} {3:HH:mm:ss} {4:0.00}h | {5:HH:mm:ss} {6:HH:mm:ss} {7:0.00}h", item.ShiftFrom.Date, item.Shift.Name, item.ShiftFrom, item.ShiftUntil, item.ShiftDuration.TotalHours, item.WorkFrom, item.WorkUntil, item.WorkDuration.TotalHours);
}
}
получаем
| SHIFT | WORK
Date Shiftname | from until dur. | from until dur.
======================|=========================|=========================
2019-01-01 early | 08:00:00 16:00:00 8.00h | 15:55:00 16:00:00 0.08h
2019-01-01 late | 16:00:00 00:00:00 8.00h | 16:00:00 16:15:00 0.25h
. Пример скрипта .net