Exemple #1
0
        private DateTime GetNextDateTimeForMonthlyByWeekAndDayFrequency(DateTime currentTime, ServiceSettings settings)
        {
            var firstDayOfMonth    = new DateTime(currentTime.Year, currentTime.Month, day: 1);
            var firstWeekOfMonth   = firstDayOfMonth.AddDays((settings.DayOfWeek + 7 - firstDayOfMonth.DayOfWeek) % 7);
            var daysToAdd          = 7 * (settings.WeekOfMonth - 1);
            var dayOfMonth         = firstWeekOfMonth.AddDays(daysToAdd);
            var dayOfMonthWithTime = dayOfMonth + settings.Time;

            return(dayOfMonthWithTime);
        }
Exemple #2
0
        private DateTime GetNextSchedulingTime()
        {
            ServiceSettings settings         = this._serviceSettings;
            DateTime        currentTime      = DateTime.UtcNow;
            DateTime        settingsDateTime = settings.Date;

            switch (settings.Frequency)
            {
            case Frequency.Monthly:
            {
                DateTime next;
                if (settings.IsLastDayOfMonth)
                {
                    next = new DateTime(currentTime.Year, currentTime.Month, 1, settingsDateTime.Hour,
                                        settingsDateTime.Minute, settingsDateTime.Second);
                    next = next.AddMonths(1).AddDays(-1);

                    if (DateTime.Compare(next, currentTime) < 0)
                    {
                        next = next.AddDays(1).AddMonths(1).AddDays(-1);
                    }
                }
                else
                {
                    next = new DateTime(currentTime.Year, currentTime.Month, settingsDateTime.Day,
                                        settingsDateTime.Hour, settingsDateTime.Minute, settingsDateTime.Second);
                    if (DateTime.Compare(next, currentTime) < 0)
                    {
                        next = next.AddMonths(1);
                    }
                }

                return(next);
            }

            case Frequency.MonthlyByWeekAndDay:
            {
                var next = this.GetNextDateTimeForMonthlyByWeekAndDayFrequency(currentTime, settings);

                if (DateTime.Compare(next, currentTime) < 0)
                {
                    next = this.GetNextDateTimeForMonthlyByWeekAndDayFrequency(currentTime.AddMonths(1), settings);
                }

                return(next);
            }

            case Frequency.Weekly:
            {
                DateTime next = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, settingsDateTime.Hour, settingsDateTime.Minute, settingsDateTime.Second);
                next = next.AddDays((settings.DayOfWeek - next.DayOfWeek + 7) % 7);

                if (DateTime.Compare(next, currentTime) < 0)
                {
                    next = next.AddDays(7);
                }
                return(next);
            }

            case Frequency.Daily:
            {
                DateTime next = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, settingsDateTime.Hour, settingsDateTime.Minute, settingsDateTime.Second);

                if (DateTime.Compare(next, currentTime) < 0)
                {
                    next = next.AddDays(1);
                }
                return(next);
            }

            case Frequency.Once:
            {
                DateTime next = new DateTime(settingsDateTime.Year, settingsDateTime.Month, settingsDateTime.Day, settingsDateTime.Hour, settingsDateTime.Minute, settingsDateTime.Second);

                if (DateTime.Compare(next, currentTime) < 0)
                {
                    return(_checkpointFileDefaultDateTime);
                }
                return(next);
            }

            case Frequency.None:
            {
                return(_checkpointFileDefaultDateTime);
            }

            case Frequency.Hourly:
            {
                return(currentTime.AddMinutes(this._serviceSettings.HourlyFrequencyInMinutes));
            }

            default:
                return(_checkpointFileDefaultDateTime);
            }
        }