C# 将工作时间添加到基于工作日和工作时间的正确价格类别中+;假日

C# 将工作时间添加到基于工作日和工作时间的正确价格类别中+;假日,c#,date,datetime,enum-flags,C#,Date,Datetime,Enum Flags,我想将正确的小时数与正确的工作时间绑定在一起。工作周从周一到周日。根据一天中的时间,工作有不同的价格。基于一天中完成的时间的工作价格称为期间。我们使用的是24小时制 例如: 第一期:周一至周五,00:00-07:00 第二期:周一至周五,07:00-20:00 第三期:周一至周五,20:00-22:00 第四期:周一至周五,22:00-23:59(午夜) 第五期:周六至周日,00:00-07:00 第六期:周六至周日,07:00-22:00 第7期:周六至周日,22:00-23:59(午夜)

我想将正确的小时数与正确的工作时间绑定在一起。工作周从周一到周日。根据一天中的时间,工作有不同的价格。基于一天中完成的时间的工作价格称为期间。我们使用的是24小时制

例如:

  • 第一期:周一至周五,00:00-07:00
  • 第二期:周一至周五,07:00-20:00
  • 第三期:周一至周五,20:00-22:00
  • 第四期:周一至周五,22:00-23:59(午夜)
  • 第五期:周六至周日,00:00-07:00
  • 第六期:周六至周日,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; }
}
public class Work
{
   public Work()
   {
   }

   public string Name { get; set; }
   public DateTime Start { get; set; }
   public DateTime End { get; set; }
}
  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)
               {

               }
          }
    }
天数为int,但值将来自此枚举:

[Flags]
public enum Workweek
{
    Sunday = 1,
    Monday = 2,
    Tuesday = 4,
    Wednesday = 8,
    Thursday = 16,
    Friday = 32,
    Saturday = 64
}
当期间
Days
属性为
62
时,表示该期间的有效期为周一至周五,当天数为:
65
时,该期间为周六至周日。当天数为:
6
时,周期为周一至周二

假日
为真时,表示该期间仅在假日有效。假日优先于正常日或周末

工作班次如下所示:

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; }
}
public class Work
{
   public Work()
   {
   }

   public string Name { get; set; }
   public DateTime Start { get; set; }
   public DateTime End { get; set; }
}
  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)
               {

               }
          }
    }
假设我有一个
列表
和一个
列表
如何将工作时间绑定到正确的时间段?每个时期有不同的定价

例如:

案例1:如果您在周一15:00到23:00之间轮班,那么该轮班将如下所示:

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; }
}
public class Work
{
   public Work()
   {
   }

   public string Name { get; set; }
   public DateTime Start { get; set; }
   public DateTime End { get; set; }
}
  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)
               {

               }
          }
    }
  • 时段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小时
假日以
列表的形式列出

如何将小时数与正确的周期相匹配

到目前为止,我尝试的是:

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
    }

}
工作=新工作()
{
名称=演示,
Star=新日期时间(2017,05,02,15,00,00);
结束=新的日期时间(2017,05,02,23,00,00);
};
列表周期=新列表();
foreach(以期间为单位的var期间)
{
Workweek shiftDay=(Workweek)Enum.Parse(typeof(Workweek),work.Start.DayOfWeek,true);//我认为这是错误的,因为如果结束日期在第二天
Workweek periodDay=(Workweek)period.Days;
如果((轮班日和周期日)=轮班日)
{
//这件作品与这段时期相吻合
}
}
我想我应该使用foreach循环开始日期和结束日期之间的每一秒,并检查这一秒是否与期间日期匹配

大概是这样的:

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; }
}
public class Work
{
   public Work()
   {
   }

   public string Name { get; set; }
   public DateTime Start { get; set; }
   public DateTime End { get; set; }
}
  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)
               {

               }
          }
    }
公共静态IEnumerable每秒(此工作)
{
DateTime currentSecond=新的日期时间(work.Start.Year、work.Start.Month、work.Start.Day、work.Start.Hour、work.Start.Minute、work.Start.Second、work.Start.millis秒);

while(currentSecond试试这样的方法。我不知道为什么你需要一个自定义的工作周,因为你可以使用标准的星期几。我还是按照你的要求做了

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; }
    }


}
使用System.Collections.ObjectModel;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
命名空间控制台应用程序57
{
班级计划
{
静态void Main(字符串[]参数)
{
}
}
公课期间
{
公共静态列表周期=新列表(){
新期间(){Name=“Period1”,Days=new Workweek[]{Workweek.周一,Workweek.周二,Workweek.周三,Workweek.周四,Workweek.周五},假日=false,开始=new TimeSpan(0,0,0),结束=new TimeSpan(7,0,0)},
新期间(){Name=“Period2”,Days=new Workweek[]{Workweek.周一,Workweek.周二,Workweek.周三,Workweek.周四,Workweek.周五},假日=false,开始=new TimeSpan(7,0,0),结束=new TimeSpan(20,0,0)},
新期间(){Name=“Period3”,Days=new Workweek[]{Workweek.周一,Workweek.周二,Workweek.周三,Workweek.周四,Workweek.周五},假日=false,开始=new TimeSpan(20,0,0),结束=new TimeSpan(22,0,0)},
新期间(){Name=“Period4”,Days=new Workweek[]{Workweek.周一,Workweek.周二,Workweek.周三,Workweek.周四,Workweek.周五},假日=false,开始=new TimeSpan(22,0,0),结束=new TimeSpan(24,0,0)},
新期间(){Name=“Period5”,天=新工作周[]{Workweek.Saturday,Workweek.Sunday},假日=假,开始=新时间跨度(0,0,0),结束=新时间跨度(7,0,0)},
新期间(){Name=“Period6”,天=新工作周[]{Workweek.Saturday,Workweek.Sunday},假日=假,开始=新时间跨度(7,0,0),结束=新时间跨度(22,0,0)},
新期间(){Name=“Period7”,天=新工作周[]{Workweek.Saturday,Workweek.Sunday},假日=假,开始=新时间跨度(22,0,0),结束=新时间跨度(24,0,0)},
new Period(){Name=“Period8”,Days=null,Holiday=true,Start=new TimeSpan(0,0,0),End=new TimeSpan(24,0,0)},
};
公共字符串名称{get;set;}
公共工作周[]天{get;set;}
公共假日{get;set;}
公共时间跨度开始{get;set;}
公共时间跨度结束{get;set;}
公共静态字符串GetName(DateTime开始时间、DateTime结束时间、布尔假日)
{
字符串名称=”;
国际单项体育联合会(假日)
{
name=“Period8”;
}
其他的
{
foreach(以期间为单位的期间)
{
布尔dayMatch=期间.Days.选择(x