Example #1
0
        public virtual DateTime?FindAfter(DateTime dt)
        {
            if (Months == ScheduleMonths.None || Days == ScheduleDays.None || Times.Count == 0)
            {
                return(null);
            }

            TimeSpan ts;

            Validate(ref dt, out ts);

            try
            {
                var future = false;

                for (var year = dt.Year; year <= dt.Year + 1; year++)
                {
                    for (var month = future ? 1 : dt.Month; month <= 12; month++)
                    {
                        if (!HasMonth(Schedules.ConvertMonth(month)))
                        {
                            future = true;
                            continue;
                        }

                        var start = new DateTime(year, month, future ? 1 : dt.Day, 0, 0, 0, dt.Kind);
                        var end   = new DateTime(year, month, DateTime.DaysInMonth(year, month), 0, 0, 0, dt.Kind);

                        for (var date = start; date <= end; date += _OneDay)
                        {
                            if (!HasDay(Schedules.ConvertDay(date.DayOfWeek)))
                            {
                                future = true;
                                continue;
                            }

                            foreach (var time in Times.Where(t => future || t > ts).OrderBy(t => t.Ticks))
                            {
                                return(new DateTime(year, month, date.Day, time.Hours, time.Minutes, 0, dt.Kind));
                            }

                            future = true;
                        }
                    }

                    future = true;
                }
            }
            catch (Exception e)
            {
                VitaNexCore.Catch(e);
            }

            return(null);
        }
Example #2
0
        public virtual DateTime?FindBefore(DateTime dt)
        {
            if (Months == ScheduleMonths.None || Days == ScheduleDays.None || Times.Count == 0)
            {
                return(null);
            }

            TimeSpan ts;

            Validate(ref dt, out ts);

            try
            {
                var past = false;

                for (var year = dt.Year; year >= dt.Year - 1; year--)
                {
                    for (var month = past ? 12 : dt.Month; month >= 1; month--)
                    {
                        if (!HasMonth(Schedules.ConvertMonth(month)))
                        {
                            past = true;
                            continue;
                        }

                        var start = new DateTime(year, month, past ? DateTime.DaysInMonth(year, month) : dt.Day, 0, 0, 0, dt.Kind);
                        var end   = new DateTime(year, month, 1, 0, 0, 0, dt.Kind);

                        for (var date = start; date >= end; date -= _OneDay)
                        {
                            if (!HasDay(Schedules.ConvertDay(date.DayOfWeek)))
                            {
                                past = true;
                                continue;
                            }

                            foreach (var time in Times.Where(t => past || t < ts).OrderByDescending(t => t.Ticks))
                            {
                                return(new DateTime(year, month, date.Day, time.Hours, time.Minutes, 0, dt.Kind));
                            }

                            past = true;
                        }
                    }

                    past = true;
                }
            }
            catch (Exception e)
            {
                VitaNexCore.Catch(e);
            }

            return(null);
        }