添加工作时间以根据工作日和工作时间 + 假期更正价格类别
Add working hours to correct price category based on week day and hours + holiday
我想将正确的小时数绑定到正确的工作时间段。工作周是从周一到周日。根据一天中的时间,作品有不同的价格。基于一天中完成时间的工作价格称为期间。我们使用 24 小时制时间格式。
示例:
- 时段 1:周一至周五,00:00 - 07:00
- 时段 2:周一至周五,07:00 - 20:00
- 时段 3:周一至周五,20:00 - 22:00
- 时段 4:周一至周五,22:00 - 23:59(午夜)
- 时段 5:周六 - 周日,00:00 - 07:00
- 第 6 期:周六 - 周日,07:00 - 22:00
- 时段 7:周六 - 周日,22:00 - 23:59(午夜)
- 第 8 期:假期,00:00 - 23:59
周期表示如下:
public class Period
{
public Period()
{
}
public string Name { get; set; }
public int Days { get; set; }
public bool Holiday { get; set; }
public TimeSpan Start { get; set; }
public TimeSpan End { get; set; }
}
天数是整数,但值将来自此枚举:
[Flags]
public enum Workweek
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64
}
当Period Days
属性为62
表示Period有效期为周一至周五,当Days为:65
Period为周六到周日。当天数为:6
期间为周一至周二。
当Holiday
为true时表示Period仅在节假日有效。假期优先于正常的一天或周末。
工作班次是这样表示的:
public class Work
{
public Work()
{
}
public string Name { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
假设我有一个 List<Work>
和一个 List<Period>
如何将工作时间绑定到正确的期间?每个时期都有不同的定价。
示例:
案例 1:如果您在周一轮班 15:00 和 23:00 之间,那么该轮班将是这样的:
- 第 2 期:5 小时
- 第 3 期:2 小时
- 第 4 期:1 小时
案例 1.1:如果那个特定的星期一是假期,那么它将是:
- 第 8 期:8 小时
情况 2:如果相反,工作在星期一开始:20:00,并在第二天 04:00 结束,结果将是:
- 第 3 期:2 小时
- 第 4 期:2 小时
- 第 1 期:4 小时
案例 2.2:如果那个星期一是假期,它将是:
- 第 8 期:4 小时
- 第 1 期:4 小时
节假日为List<DateTime>
。
我怎样才能将时间与更正的经期相匹配?
到目前为止我尝试了什么:
Work work = new Work()
{
Name = Demo,
Star = new DateTime(2017,05,02,15,00,00);
End = new DateTime(2017,05,02,23,00,00);
};
List<Period> periods = new List<Period>();
foreach (var period in periods)
{
Workweek shiftDay = (Workweek)Enum.Parse(typeof(Workweek), work.Start.DayOfWeek, true); // i think this is wrong, because in the case where the end date is in the next day
Workweek periodDay = (Workweek)period.Days;
if ((shiftDay & periodDay) == shiftDay)
{
// the work matches the period
}
}
我在想我应该使用 foreach 并循环遍历开始日期和结束日期之间的每一秒,并检查该秒是否与周期日匹配。
像这样:
public static IEnumerable<DateTime> EachSecond(this Work work)
{
DateTime currentSecond = new DateTime(work.Start.Year, work.Start.Month, work.Start.Day, work.Start.Hour, work.Start.Minute, work.Start.Second, work.Start.Millisecond);
while (currentSecond <= wor.kEnd)
{
yield return currentSecond;
currentSecond = currentSecond.AddSeconds(1);
}
}
Work work = new Work()
{
Name = Demo,
Star = new DateTime(2017,05,02,15,00,00);
End = new DateTime(2017,05,02,23,00,00);
};
List<Period> periods = new List<Period>();
foreach (var second in work.EachSecond())
{
Workweek shiftSecond = (Workweek)Enum.Parse(typeof(Workweek), second.DayOfWeek, true);
foreach (var period in periods)
{
Workweek periodDay = (Workweek)period.Days;
if ((shiftSecond & periodDay) == shiftSecond)
{
}
}
}
尝试这样的事情。我不知道当您可以使用标准 DayOfWeek 时为什么需要自定义工作周。无论如何我都按照你的要求做了。
using System.Collections.ObjectModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication57
{
class Program
{
static void Main(string[] args)
{
}
}
public class Period
{
public static List<Period> periods = new List<Period>() {
new Period() { Name = "Period1", Days = new Workweek[] { Workweek.Monday, Workweek.Tuesday, Workweek.Wednesday, Workweek.Thursday, Workweek.Friday}, Holiday = false, Start = new TimeSpan(0,0,0), End = new TimeSpan(7,0,0)},
new Period() { Name = "Period2", Days = new Workweek[] { Workweek.Monday, Workweek.Tuesday, Workweek.Wednesday, Workweek.Thursday, Workweek.Friday}, Holiday = false, Start = new TimeSpan(7,0,0), End = new TimeSpan(20,0,0)},
new Period() { Name = "Period3", Days = new Workweek[] { Workweek.Monday, Workweek.Tuesday, Workweek.Wednesday, Workweek.Thursday, Workweek.Friday}, Holiday = false, Start = new TimeSpan(20,0,0), End = new TimeSpan(22,0,0)},
new Period() { Name = "Period4", Days = new Workweek[] { Workweek.Monday, Workweek.Tuesday, Workweek.Wednesday, Workweek.Thursday, Workweek.Friday}, Holiday = false, Start = new TimeSpan(22,0,0), End = new TimeSpan(24,0,0)},
new Period() { Name = "Period5", Days = new Workweek[] { Workweek.Saturday, Workweek.Sunday}, Holiday = false, Start = new TimeSpan(0,0,0), End = new TimeSpan(7,0,0)},
new Period() { Name = "Period6", Days = new Workweek[] { Workweek.Saturday, Workweek.Sunday}, Holiday = false, Start = new TimeSpan(7,0,0), End = new TimeSpan(22,0,0)},
new Period() { Name = "Period7", Days = new Workweek[] { Workweek.Saturday, Workweek.Sunday}, Holiday = false, Start = new TimeSpan(22,0,0), End = new TimeSpan(24,0,0)},
new Period() { Name = "Period8", Days = null, Holiday = true, Start = new TimeSpan(0,0,0), End = new TimeSpan(24,0,0)},
};
public string Name { get; set; }
public Workweek[] Days { get; set; }
public bool Holiday { get; set; }
public TimeSpan Start { get; set; }
public TimeSpan End { get; set; }
public static string GetName(DateTime startTime, DateTime endTime, Boolean holiday)
{
string name = "";
if (holiday)
{
name = "Period8";
}
else
{
foreach (Period period in periods)
{
Boolean dayMatch = period.Days.Select(x => x == (Workweek)(2 ^ (int)startTime.DayOfWeek)).Any();
if (dayMatch)
{
if ((startTime.TimeOfDay > period.Start) && (endTime.TimeOfDay < period.End))
{
name = period.Name;
break;
}
}
}
}
return name;
}
}
public enum Workweek
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64
}
public class Work
{
public string Name { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
}
工作示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
public class Program
{
public static void Main( string[] args )
{
var periods = new List<Period> {
new Period( "1", Days.Workdays, TimeSpan.FromHours(0), TimeSpan.FromHours(7) ),
new Period( "2", Days.Workdays, TimeSpan.FromHours(7), TimeSpan.FromHours(20) ),
new Period( "3", Days.Workdays, TimeSpan.FromHours(20), TimeSpan.FromHours(22) ),
new Period( "4", Days.Workdays, TimeSpan.FromHours(22), TimeSpan.FromHours(24) ),
new Period( "5", Days.Weekend, TimeSpan.FromHours(0), TimeSpan.FromHours(7) ),
new Period( "6", Days.Weekend, TimeSpan.FromHours(7), TimeSpan.FromHours(22) ),
new Period( "7", Days.Weekend, TimeSpan.FromHours(22), TimeSpan.FromHours(24) ),
new Period( "8", Days.Holiday, TimeSpan.FromHours(0), TimeSpan.FromHours(24) ),
};
var holidays = new List<DateTime> {
new DateTime( 2017, 1, 1 ),
new DateTime( 2017, 1, 3 ),
new DateTime( 2017, 1, 6 ),
};
var sc = new ShiftCalculator( periods, holidays );
var shiftperiods = sc.GetShiftPeriods( new DateTime( 2016, 12, 31, 22, 00, 00 ), new DateTime( 2017, 01, 07, 08, 00, 00 ) ).ToList();
foreach ( var sp in shiftperiods )
{
Console.WriteLine( "{0} - {1} - {2} - {3:00.00}h", sp.Period.Name, sp.Period.Days, sp.Start, sp.Duration.TotalHours );
}
}
}
[Flags]
enum Days : byte
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64,
Holiday = 128,
Workdays = Monday | Tuesday | Wednesday | Thursday | Friday,
Weekend = Saturday | Sunday,
}
[DebuggerDisplay("{Name}: {Days} ({Start}-{End})")]
class Period
{
public Period( string name, Days days, TimeSpan start, TimeSpan end )
{
if ( days.HasFlag( Days.Holiday ) && days != Days.Holiday )
throw new ArgumentException( "days" );
if ( start > end )
throw new ArgumentException( "end" );
Name = name;
Days = days;
Start = start;
End = end;
}
public string Name { get; private set; }
public Days Days { get; private set; }
public TimeSpan Start { get; private set; }
public TimeSpan End { get; private set; }
}
class ShiftPeriod
{
public Period Period { get; set; }
public DateTime Start { get; set; }
public TimeSpan Duration { get; set; }
}
class ShiftCalculator
{
private readonly List<Period> _periods;
private readonly List<DateTime> _holidays;
public ShiftCalculator( IEnumerable<Period> periods, IEnumerable<DateTime> holidays )
{
_periods = periods.ToList();
_holidays = holidays.Select( e => e.Date ).ToList();
}
public IEnumerable<ShiftPeriod> GetShiftPeriods( DateTime start, DateTime end )
{
if ( start > end ) throw new ArgumentException( "end" );
var current = start;
while ( current < end )
{
var period = GetPeriodByDateTime( current );
var next = current.Date + period.End;
if ( next > end )
{
next = end;
}
yield return new ShiftPeriod
{
Period = period,
Start = current,
Duration = next - current,
};
current = next;
}
}
private Days GetDayFromDateTime( DateTime datetime )
{
Days day;
if ( _holidays.Contains( datetime.Date ) )
{
day = Days.Holiday;
}
else
{
switch ( datetime.DayOfWeek )
{
case DayOfWeek.Sunday:
day = Days.Sunday;
break;
case DayOfWeek.Monday:
day = Days.Monday;
break;
case DayOfWeek.Tuesday:
day = Days.Tuesday;
break;
case DayOfWeek.Wednesday:
day = Days.Wednesday;
break;
case DayOfWeek.Thursday:
day = Days.Thursday;
break;
case DayOfWeek.Friday:
day = Days.Friday;
break;
case DayOfWeek.Saturday:
day = Days.Saturday;
break;
default:
throw new InvalidOperationException();
}
}
return day;
}
private Period GetPeriodByDateTime( DateTime datetime )
{
var day = GetDayFromDateTime( datetime );
var timeOfDay = datetime.TimeOfDay;
var period = _periods.Where(
e => e.Days.HasFlag( day ) && e.Start <= timeOfDay && e.End > timeOfDay )
.FirstOrDefault();
if ( period == null )
{
throw new InvalidOperationException();
}
return period;
}
}
生成的输出
7 - Weekend - 12/31/2016 10:00:00 PM - 02.00h
8 - Holiday - 1/1/2017 12:00:00 AM - 24.00h
1 - Workdays - 1/2/2017 12:00:00 AM - 07.00h
2 - Workdays - 1/2/2017 7:00:00 AM - 13.00h
3 - Workdays - 1/2/2017 8:00:00 PM - 02.00h
4 - Workdays - 1/2/2017 10:00:00 PM - 02.00h
8 - Holiday - 1/3/2017 12:00:00 AM - 24.00h
1 - Workdays - 1/4/2017 12:00:00 AM - 07.00h
2 - Workdays - 1/4/2017 7:00:00 AM - 13.00h
3 - Workdays - 1/4/2017 8:00:00 PM - 02.00h
4 - Workdays - 1/4/2017 10:00:00 PM - 02.00h
1 - Workdays - 1/5/2017 12:00:00 AM - 07.00h
2 - Workdays - 1/5/2017 7:00:00 AM - 13.00h
3 - Workdays - 1/5/2017 8:00:00 PM - 02.00h
4 - Workdays - 1/5/2017 10:00:00 PM - 02.00h
8 - Holiday - 1/6/2017 12:00:00 AM - 24.00h
5 - Weekend - 1/7/2017 12:00:00 AM - 07.00h
6 - Weekend - 1/7/2017 7:00:00 AM - 01.00h
我想将正确的小时数绑定到正确的工作时间段。工作周是从周一到周日。根据一天中的时间,作品有不同的价格。基于一天中完成时间的工作价格称为期间。我们使用 24 小时制时间格式。
示例:
- 时段 1:周一至周五,00:00 - 07:00
- 时段 2:周一至周五,07:00 - 20:00
- 时段 3:周一至周五,20:00 - 22:00
- 时段 4:周一至周五,22:00 - 23:59(午夜)
- 时段 5:周六 - 周日,00:00 - 07:00
- 第 6 期:周六 - 周日,07:00 - 22:00
- 时段 7:周六 - 周日,22:00 - 23:59(午夜)
- 第 8 期:假期,00:00 - 23:59
周期表示如下:
public class Period
{
public Period()
{
}
public string Name { get; set; }
public int Days { get; set; }
public bool Holiday { get; set; }
public TimeSpan Start { get; set; }
public TimeSpan End { get; set; }
}
天数是整数,但值将来自此枚举:
[Flags]
public enum Workweek
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64
}
当Period Days
属性为62
表示Period有效期为周一至周五,当Days为:65
Period为周六到周日。当天数为:6
期间为周一至周二。
当Holiday
为true时表示Period仅在节假日有效。假期优先于正常的一天或周末。
工作班次是这样表示的:
public class Work
{
public Work()
{
}
public string Name { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
假设我有一个 List<Work>
和一个 List<Period>
如何将工作时间绑定到正确的期间?每个时期都有不同的定价。
示例:
案例 1:如果您在周一轮班 15:00 和 23:00 之间,那么该轮班将是这样的:
- 第 2 期:5 小时
- 第 3 期:2 小时
- 第 4 期:1 小时
案例 1.1:如果那个特定的星期一是假期,那么它将是:
- 第 8 期:8 小时
情况 2:如果相反,工作在星期一开始:20:00,并在第二天 04:00 结束,结果将是:
- 第 3 期:2 小时
- 第 4 期:2 小时
- 第 1 期:4 小时
案例 2.2:如果那个星期一是假期,它将是:
- 第 8 期:4 小时
- 第 1 期:4 小时
节假日为List<DateTime>
。
我怎样才能将时间与更正的经期相匹配?
到目前为止我尝试了什么:
Work work = new Work()
{
Name = Demo,
Star = new DateTime(2017,05,02,15,00,00);
End = new DateTime(2017,05,02,23,00,00);
};
List<Period> periods = new List<Period>();
foreach (var period in periods)
{
Workweek shiftDay = (Workweek)Enum.Parse(typeof(Workweek), work.Start.DayOfWeek, true); // i think this is wrong, because in the case where the end date is in the next day
Workweek periodDay = (Workweek)period.Days;
if ((shiftDay & periodDay) == shiftDay)
{
// the work matches the period
}
}
我在想我应该使用 foreach 并循环遍历开始日期和结束日期之间的每一秒,并检查该秒是否与周期日匹配。
像这样:
public static IEnumerable<DateTime> EachSecond(this Work work)
{
DateTime currentSecond = new DateTime(work.Start.Year, work.Start.Month, work.Start.Day, work.Start.Hour, work.Start.Minute, work.Start.Second, work.Start.Millisecond);
while (currentSecond <= wor.kEnd)
{
yield return currentSecond;
currentSecond = currentSecond.AddSeconds(1);
}
}
Work work = new Work()
{
Name = Demo,
Star = new DateTime(2017,05,02,15,00,00);
End = new DateTime(2017,05,02,23,00,00);
};
List<Period> periods = new List<Period>();
foreach (var second in work.EachSecond())
{
Workweek shiftSecond = (Workweek)Enum.Parse(typeof(Workweek), second.DayOfWeek, true);
foreach (var period in periods)
{
Workweek periodDay = (Workweek)period.Days;
if ((shiftSecond & periodDay) == shiftSecond)
{
}
}
}
尝试这样的事情。我不知道当您可以使用标准 DayOfWeek 时为什么需要自定义工作周。无论如何我都按照你的要求做了。
using System.Collections.ObjectModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication57
{
class Program
{
static void Main(string[] args)
{
}
}
public class Period
{
public static List<Period> periods = new List<Period>() {
new Period() { Name = "Period1", Days = new Workweek[] { Workweek.Monday, Workweek.Tuesday, Workweek.Wednesday, Workweek.Thursday, Workweek.Friday}, Holiday = false, Start = new TimeSpan(0,0,0), End = new TimeSpan(7,0,0)},
new Period() { Name = "Period2", Days = new Workweek[] { Workweek.Monday, Workweek.Tuesday, Workweek.Wednesday, Workweek.Thursday, Workweek.Friday}, Holiday = false, Start = new TimeSpan(7,0,0), End = new TimeSpan(20,0,0)},
new Period() { Name = "Period3", Days = new Workweek[] { Workweek.Monday, Workweek.Tuesday, Workweek.Wednesday, Workweek.Thursday, Workweek.Friday}, Holiday = false, Start = new TimeSpan(20,0,0), End = new TimeSpan(22,0,0)},
new Period() { Name = "Period4", Days = new Workweek[] { Workweek.Monday, Workweek.Tuesday, Workweek.Wednesday, Workweek.Thursday, Workweek.Friday}, Holiday = false, Start = new TimeSpan(22,0,0), End = new TimeSpan(24,0,0)},
new Period() { Name = "Period5", Days = new Workweek[] { Workweek.Saturday, Workweek.Sunday}, Holiday = false, Start = new TimeSpan(0,0,0), End = new TimeSpan(7,0,0)},
new Period() { Name = "Period6", Days = new Workweek[] { Workweek.Saturday, Workweek.Sunday}, Holiday = false, Start = new TimeSpan(7,0,0), End = new TimeSpan(22,0,0)},
new Period() { Name = "Period7", Days = new Workweek[] { Workweek.Saturday, Workweek.Sunday}, Holiday = false, Start = new TimeSpan(22,0,0), End = new TimeSpan(24,0,0)},
new Period() { Name = "Period8", Days = null, Holiday = true, Start = new TimeSpan(0,0,0), End = new TimeSpan(24,0,0)},
};
public string Name { get; set; }
public Workweek[] Days { get; set; }
public bool Holiday { get; set; }
public TimeSpan Start { get; set; }
public TimeSpan End { get; set; }
public static string GetName(DateTime startTime, DateTime endTime, Boolean holiday)
{
string name = "";
if (holiday)
{
name = "Period8";
}
else
{
foreach (Period period in periods)
{
Boolean dayMatch = period.Days.Select(x => x == (Workweek)(2 ^ (int)startTime.DayOfWeek)).Any();
if (dayMatch)
{
if ((startTime.TimeOfDay > period.Start) && (endTime.TimeOfDay < period.End))
{
name = period.Name;
break;
}
}
}
}
return name;
}
}
public enum Workweek
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64
}
public class Work
{
public string Name { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
}
工作示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
public class Program
{
public static void Main( string[] args )
{
var periods = new List<Period> {
new Period( "1", Days.Workdays, TimeSpan.FromHours(0), TimeSpan.FromHours(7) ),
new Period( "2", Days.Workdays, TimeSpan.FromHours(7), TimeSpan.FromHours(20) ),
new Period( "3", Days.Workdays, TimeSpan.FromHours(20), TimeSpan.FromHours(22) ),
new Period( "4", Days.Workdays, TimeSpan.FromHours(22), TimeSpan.FromHours(24) ),
new Period( "5", Days.Weekend, TimeSpan.FromHours(0), TimeSpan.FromHours(7) ),
new Period( "6", Days.Weekend, TimeSpan.FromHours(7), TimeSpan.FromHours(22) ),
new Period( "7", Days.Weekend, TimeSpan.FromHours(22), TimeSpan.FromHours(24) ),
new Period( "8", Days.Holiday, TimeSpan.FromHours(0), TimeSpan.FromHours(24) ),
};
var holidays = new List<DateTime> {
new DateTime( 2017, 1, 1 ),
new DateTime( 2017, 1, 3 ),
new DateTime( 2017, 1, 6 ),
};
var sc = new ShiftCalculator( periods, holidays );
var shiftperiods = sc.GetShiftPeriods( new DateTime( 2016, 12, 31, 22, 00, 00 ), new DateTime( 2017, 01, 07, 08, 00, 00 ) ).ToList();
foreach ( var sp in shiftperiods )
{
Console.WriteLine( "{0} - {1} - {2} - {3:00.00}h", sp.Period.Name, sp.Period.Days, sp.Start, sp.Duration.TotalHours );
}
}
}
[Flags]
enum Days : byte
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64,
Holiday = 128,
Workdays = Monday | Tuesday | Wednesday | Thursday | Friday,
Weekend = Saturday | Sunday,
}
[DebuggerDisplay("{Name}: {Days} ({Start}-{End})")]
class Period
{
public Period( string name, Days days, TimeSpan start, TimeSpan end )
{
if ( days.HasFlag( Days.Holiday ) && days != Days.Holiday )
throw new ArgumentException( "days" );
if ( start > end )
throw new ArgumentException( "end" );
Name = name;
Days = days;
Start = start;
End = end;
}
public string Name { get; private set; }
public Days Days { get; private set; }
public TimeSpan Start { get; private set; }
public TimeSpan End { get; private set; }
}
class ShiftPeriod
{
public Period Period { get; set; }
public DateTime Start { get; set; }
public TimeSpan Duration { get; set; }
}
class ShiftCalculator
{
private readonly List<Period> _periods;
private readonly List<DateTime> _holidays;
public ShiftCalculator( IEnumerable<Period> periods, IEnumerable<DateTime> holidays )
{
_periods = periods.ToList();
_holidays = holidays.Select( e => e.Date ).ToList();
}
public IEnumerable<ShiftPeriod> GetShiftPeriods( DateTime start, DateTime end )
{
if ( start > end ) throw new ArgumentException( "end" );
var current = start;
while ( current < end )
{
var period = GetPeriodByDateTime( current );
var next = current.Date + period.End;
if ( next > end )
{
next = end;
}
yield return new ShiftPeriod
{
Period = period,
Start = current,
Duration = next - current,
};
current = next;
}
}
private Days GetDayFromDateTime( DateTime datetime )
{
Days day;
if ( _holidays.Contains( datetime.Date ) )
{
day = Days.Holiday;
}
else
{
switch ( datetime.DayOfWeek )
{
case DayOfWeek.Sunday:
day = Days.Sunday;
break;
case DayOfWeek.Monday:
day = Days.Monday;
break;
case DayOfWeek.Tuesday:
day = Days.Tuesday;
break;
case DayOfWeek.Wednesday:
day = Days.Wednesday;
break;
case DayOfWeek.Thursday:
day = Days.Thursday;
break;
case DayOfWeek.Friday:
day = Days.Friday;
break;
case DayOfWeek.Saturday:
day = Days.Saturday;
break;
default:
throw new InvalidOperationException();
}
}
return day;
}
private Period GetPeriodByDateTime( DateTime datetime )
{
var day = GetDayFromDateTime( datetime );
var timeOfDay = datetime.TimeOfDay;
var period = _periods.Where(
e => e.Days.HasFlag( day ) && e.Start <= timeOfDay && e.End > timeOfDay )
.FirstOrDefault();
if ( period == null )
{
throw new InvalidOperationException();
}
return period;
}
}
生成的输出
7 - Weekend - 12/31/2016 10:00:00 PM - 02.00h 8 - Holiday - 1/1/2017 12:00:00 AM - 24.00h 1 - Workdays - 1/2/2017 12:00:00 AM - 07.00h 2 - Workdays - 1/2/2017 7:00:00 AM - 13.00h 3 - Workdays - 1/2/2017 8:00:00 PM - 02.00h 4 - Workdays - 1/2/2017 10:00:00 PM - 02.00h 8 - Holiday - 1/3/2017 12:00:00 AM - 24.00h 1 - Workdays - 1/4/2017 12:00:00 AM - 07.00h 2 - Workdays - 1/4/2017 7:00:00 AM - 13.00h 3 - Workdays - 1/4/2017 8:00:00 PM - 02.00h 4 - Workdays - 1/4/2017 10:00:00 PM - 02.00h 1 - Workdays - 1/5/2017 12:00:00 AM - 07.00h 2 - Workdays - 1/5/2017 7:00:00 AM - 13.00h 3 - Workdays - 1/5/2017 8:00:00 PM - 02.00h 4 - Workdays - 1/5/2017 10:00:00 PM - 02.00h 8 - Holiday - 1/6/2017 12:00:00 AM - 24.00h 5 - Weekend - 1/7/2017 12:00:00 AM - 07.00h 6 - Weekend - 1/7/2017 7:00:00 AM - 01.00h