Ejemplo n.º 1
0
        /// <summary>
        /// 创建日程单元
        /// </summary>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="cycleType">循环类型</param>
        /// <param name="week">星期 cycleType为周循环有效</param>
        /// <returns></returns>
        public static ScheduleUnit Create(DateTime start, DateTime end
                                          , ScheduleCycleType cycleType = ScheduleCycleType.None, int week = 0)
        {
            ScheduleUnit unit = new ScheduleUnit();

            return((unit.UpdateData(start, end, cycleType, week)) ? unit : null);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 更新日程
 /// </summary>
 /// <param name="start"></param>
 /// <param name="end"></param>
 /// <param name="cycleType"></param>
 /// <param name="week"></param>
 /// <returns></returns>
 public bool UpdateData(DateTime start, DateTime end, ScheduleCycleType cycleType, int week = 0)
 {
     if (null == start || null == end ||
         end.Ticks - start.Ticks < 0)
     {
         return(false);
     }
     this.start     = start;
     this.end       = end;
     this.cycleType = cycleType;
     this.week      = week;
     return(true);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// dateTime是否在循环日程中
        /// </summary>
        /// <param name="start">日程开始时间</param>
        /// <param name="end">日程结束时间</param>
        /// <param name="dateTime">当前时间</param>
        /// <param name="leftseconds">如果在日程内leftseonds表示本次日程剩余时间,
        /// 反之距离下次日程时间,如果为0:表示不在日程内并且没有下个日程</param>
        /// <param name="cycleType"></param>
        /// <param name="week"></param>
        /// <returns></returns>
        public static bool IsInCycleDateTme(DateTime start, DateTime end
                                            , DateTime dateTime, out long leftseconds
                                            , ScheduleCycleType cycleType = ScheduleCycleType.None, DayOfWeek week = 0)
        {
            leftseconds = 0;
            bool inschedule = false;

            if (DateTimeHelper.IsDateTimeBetweenStartAndEnd(start, end, dateTime))
            {
                DateTime startTemp;
                DateTime endTemp;
                switch (cycleType)
                {
                case ScheduleCycleType.None:
                {
                    inschedule  = true;
                    leftseconds = (long)((end - dateTime).TotalSeconds);
                }
                break;

                case ScheduleCycleType.Day:
                {
                    startTemp = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, start.Hour, start.Minute, 0, 0, 0);
                    endTemp   = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, end.Hour, end.Minute, 0, 0, 0);
                    if (startTemp > endTemp)
                    {
                        Engine.Utility.Log.Error("ScheduleUnit-> IsInSchedule ScheduleCycleType.Day error startTemp>endTemp");
                    }
                    else
                    {
                        if (DateTimeHelper.IsDateTimeBetweenStartAndEnd(startTemp, endTemp, dateTime))
                        {
                            inschedule  = true;
                            leftseconds = (long)((endTemp - dateTime).TotalSeconds);
                        }
                        else if (dateTime > end)
                        {
                            startTemp = startTemp.AddDays(1);
                            if (dateTime < startTemp)
                            {
                                //距离下一个日程
                                inschedule  = false;
                                leftseconds = (long)((startTemp - dateTime).TotalSeconds);
                            }
                        }
                        else if (dateTime < startTemp)
                        {
                            inschedule  = false;
                            leftseconds = (long)((startTemp - dateTime).TotalSeconds);
                        }
                    }
                }
                break;

                case ScheduleCycleType.Week:
                {
                    startTemp = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, start.Hour, start.Minute, 0, 0, 0);
                    endTemp   = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, end.Hour, end.Minute, 0, 0, 0);
                    DayOfWeek dw = dateTime.DayOfWeek;
                    if (dw == week)
                    {
                        if (DateTimeHelper.IsDateTimeBetweenStartAndEnd(startTemp, endTemp, dateTime))
                        {
                            inschedule  = true;
                            leftseconds = (long)((endTemp - dateTime).TotalSeconds);
                        }
                        else if (dateTime < startTemp)
                        {
                            inschedule  = false;
                            leftseconds = (long)((startTemp - dateTime).TotalSeconds);
                        }
                        else
                        {
                            //往后推一星期
                            startTemp = startTemp.AddDays(7);
                            if (DateTimeHelper.IsDateTimeBetweenStartAndEnd(start, end, startTemp))
                            {
                                inschedule  = false;
                                leftseconds = (long)((startTemp - dateTime).TotalSeconds);
                            }
                        }
                    }
                    else
                    {
                        //直接找下一个满足条件的星期
                        int gapDay = week - dw;
                        if (gapDay <= 0)
                        {
                            gapDay += 7;
                        }
                        startTemp = startTemp.AddDays(gapDay);
                        if (DateTimeHelper.IsDateTimeBetweenStartAndEnd(start, end, startTemp))
                        {
                            inschedule  = false;
                            leftseconds = (long)((startTemp - dateTime).TotalSeconds);
                        }
                    }
                }
                break;

                case ScheduleCycleType.Month:
                {
                    startTemp = new DateTime(dateTime.Year, dateTime.Month, start.Day, start.Hour, start.Minute, 0, 0, 0);
                    endTemp   = new DateTime(dateTime.Year, dateTime.Month, end.Day, end.Hour, end.Minute, 0, 0, 0);
                    if (startTemp > endTemp)
                    {
                        Engine.Utility.Log.Error("ScheduleUnit-> HandlerScheduleMonth error startTemp>endTemp");
                    }
                    else
                    {
                        if (DateTimeHelper.IsDateTimeBetweenStartAndEnd(startTemp, endTemp, dateTime))
                        {
                            inschedule  = true;
                            leftseconds = (long)((endTemp - dateTime).TotalSeconds);
                        }
                        else if (dateTime > end)
                        {
                            startTemp = startTemp.AddMonths(1);
                            if (DateTimeHelper.IsDateTimeBetweenStartAndEnd(start, end, startTemp) &&
                                dateTime < startTemp)
                            {
                                //距离下一个日程
                                inschedule  = false;
                                leftseconds = (long)((startTemp - dateTime).TotalSeconds);
                            }
                        }
                        else if (dateTime < startTemp)
                        {
                            inschedule  = false;
                            leftseconds = (long)((startTemp - dateTime).TotalSeconds);
                        }
                    }
                }
                break;

                case ScheduleCycleType.Year:
                {
                    startTemp = new DateTime(dateTime.Year, start.Month, start.Day, start.Hour, start.Minute, 0, 0, 0);
                    endTemp   = new DateTime(dateTime.Year, end.Month, end.Day, end.Hour, end.Minute, 0, 0, 0);
                    if (startTemp > endTemp)
                    {
                        Engine.Utility.Log.Error("ScheduleUnit-> HandlerScheduleYear error startTemp>endTemp");
                    }
                    else
                    {
                        if (DateTimeHelper.IsDateTimeBetweenStartAndEnd(startTemp, endTemp, dateTime))
                        {
                            inschedule  = true;
                            leftseconds = (long)((endTemp - dateTime).TotalSeconds);
                        }
                        else if (dateTime > end)
                        {
                            startTemp = startTemp.AddYears(1);
                            if (DateTimeHelper.IsDateTimeBetweenStartAndEnd(start, end, startTemp) &&
                                dateTime < startTemp)
                            {
                                //距离下一个日程
                                inschedule  = false;
                                leftseconds = (long)((startTemp - dateTime).TotalSeconds);
                            }
                        }
                        else if (dateTime < startTemp)
                        {
                            inschedule  = false;
                            leftseconds = (long)((startTemp - dateTime).TotalSeconds);
                        }
                    }
                }
                break;
                }
            }
            else if (dateTime < start)
            {
                leftseconds = (long)((start - dateTime).TotalSeconds);
            }
            return(inschedule);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// dateTime是否在循环日程中
 /// </summary>
 /// <param name="start">日程开始时间</param>
 /// <param name="end">日程结束时间</param>
 /// <param name="dateTime">当前时间</param>
 /// <param name="leftseconds">如果在日程内leftseonds表示本次日程剩余时间,
 /// 反之距离下次日程时间,如果为0:表示不在日程内并且没有下个日程</param>
 /// <param name="cycleType"></param>
 /// <param name="week"></param>
 /// <returns></returns>
 public static bool IsInCycleDateTme(long start, long end, long datetime, out long leftseconds, ScheduleCycleType cycleType = ScheduleCycleType.None, DayOfWeek week = 0)
 {
     return(IsInCycleDateTme(DateTimeHelper.TransformServerTime2DateTime(start)
                             , DateTimeHelper.TransformServerTime2DateTime(end)
                             , DateTimeHelper.TransformServerTime2DateTime(datetime)
                             , out leftseconds, cycleType, week));
 }