private long ComputeNextSetLastScheduled(long currentTime)
        {
            // handle already-stopped
            if (_cachedCountRepeated == long.MaxValue)
            {
                return(-1);
            }

            // handle date-only-form: "<date>"
            if (_spec.OptionalRepeatCount == null && _spec.OptionalDate != null && _spec.OptionalTimePeriod == null)
            {
                _cachedCountRepeated = long.MaxValue;
                var anchorTimeInMillis = _anchorTime.TimeInMillis;
                if (anchorTimeInMillis > currentTime)
                {
                    return(anchorTimeInMillis - currentTime);
                }
                return(-1);
            }

            // handle period-only-form: "P<period>"
            // handle partial-form-2: "<date>/<period>" (non-recurring)
            if (_spec.OptionalRepeatCount == null && _spec.OptionalTimePeriod != null)
            {
                _cachedCountRepeated = long.MaxValue;
                _cachedLastScheduled = new DateTimeEx(_anchorTime);
                CalendarOpPlusMinus.Action(_cachedLastScheduled, 1, _spec.OptionalTimePeriod);
                var cachedLastTimeInMillis = _cachedLastScheduled.TimeInMillis;
                if (cachedLastTimeInMillis > currentTime)
                {
                    return(cachedLastTimeInMillis - currentTime);
                }
                return(-1);
            }

            // handle partial-form-1: "R<?>/<period>"
            // handle full form
            if (_cachedLastScheduled == null)
            {
                _cachedLastScheduled = new DateTimeEx(_anchorTime);
                if (_spec.OptionalDate != null)
                {
                    _cachedCountRepeated = 1;
                }
            }

            if (_spec.OptionalRepeatCount == -1)
            {
                var nextDueInner = CalendarOpPlusFastAddHelper.ComputeNextDue(currentTime, _spec.OptionalTimePeriod, _cachedLastScheduled);
                _cachedLastScheduled.Set(nextDueInner.Scheduled);
                return(_cachedLastScheduled.TimeInMillis - currentTime);
            }

            var nextDue = CalendarOpPlusFastAddHelper.ComputeNextDue(currentTime, _spec.OptionalTimePeriod, _cachedLastScheduled);

            _cachedCountRepeated += nextDue.Factor;
            if (_cachedCountRepeated <= _spec.OptionalRepeatCount)
            {
                _cachedLastScheduled.Set(nextDue.Scheduled);
                var cachedLastTimeInMillis = _cachedLastScheduled.TimeInMillis;
                if (cachedLastTimeInMillis > currentTime)
                {
                    return(cachedLastTimeInMillis - currentTime);
                }
            }
            _cachedCountRepeated = long.MaxValue;
            return(-1);
        }
Beispiel #2
0
        private long ComputeNextSetLastScheduled(long currentTime, TimeAbacus timeAbacus)
        {
            // handle already-stopped
            if (_cachedCountRepeated == Int64.MaxValue)
            {
                return(-1);
            }

            // handle date-only-form: "<date>"
            if (_spec.OptionalRepeatCount == null && _spec.OptionalDate != null && _spec.OptionalTimePeriod == null)
            {
                _cachedCountRepeated = Int64.MaxValue;
                var computed = timeAbacus.CalendarGet(_anchorTime, _anchorRemainder);
                if (computed > currentTime)
                {
                    return(computed - currentTime);
                }
                return(-1);
            }

            // handle period-only-form: "P<period>"
            // handle partial-form-2: "<date>/<period>" (non-recurring)
            if (_spec.OptionalRepeatCount == null && _spec.OptionalTimePeriod != null)
            {
                _cachedCountRepeated = Int64.MaxValue;
                _cachedLastScheduled = _anchorTime.Clone();
                CalendarOpPlusMinus.Action(_cachedLastScheduled, 1, _spec.OptionalTimePeriod);
                var computed = timeAbacus.CalendarGet(_cachedLastScheduled, _anchorRemainder);
                if (computed > currentTime)
                {
                    return(computed - currentTime);
                }
                return(-1);
            }

            // handle partial-form-1: "R<?>/<period>"
            // handle full form
            if (_cachedLastScheduled == null)
            {
                _cachedLastScheduled = _anchorTime.Clone();
                if (_spec.OptionalDate != null)
                {
                    _cachedCountRepeated = 1;
                }
            }

            var nextDue = CalendarOpPlusFastAddHelper.ComputeNextDue(
                currentTime, _spec.OptionalTimePeriod, _cachedLastScheduled, timeAbacus, _anchorRemainder);

            if (_spec.OptionalRepeatCount == -1)
            {
                _cachedLastScheduled = nextDue.Scheduled;
                var computed = timeAbacus.CalendarGet(_cachedLastScheduled, _anchorRemainder);
                return(computed - currentTime);
            }

            _cachedCountRepeated += nextDue.Factor;
            if (_cachedCountRepeated <= _spec.OptionalRepeatCount)
            {
                _cachedLastScheduled = nextDue.Scheduled;
                var computed = timeAbacus.CalendarGet(_cachedLastScheduled, _anchorRemainder);
                if (computed > currentTime)
                {
                    return(computed - currentTime);
                }
            }
            _cachedCountRepeated = Int64.MaxValue;
            return(-1);
        }