GetSimpleDateTimeData() 공개 정적인 메소드

public static GetSimpleDateTimeData ( IDateTime dt ) : System.DateTime
dt IDateTime
리턴 System.DateTime
예제 #1
0
        static public IList <Occurrence> GetOccurrences(IRecurrable recurrable, IDateTime periodStart, IDateTime periodEnd, bool includeReferenceDateInResults)
        {
            List <Occurrence> occurrences = new List <Occurrence>();

            IEvaluator evaluator = recurrable.GetService(typeof(IEvaluator)) as IEvaluator;

            if (evaluator != null)
            {
                // Change the time zone of periodStart/periodEnd as needed
                // so they can be used during the evaluation process.
                periodStart = DateUtil.MatchTimeZone(recurrable.Start, periodStart);
                periodEnd   = DateUtil.MatchTimeZone(recurrable.Start, periodEnd);

                IList <IPeriod> periods = evaluator.Evaluate(
                    recurrable.Start,
                    DateUtil.GetSimpleDateTimeData(periodStart),
                    DateUtil.GetSimpleDateTimeData(periodEnd),
                    includeReferenceDateInResults);

                foreach (IPeriod p in periods)
                {
                    // Filter the resulting periods to only contain those between
                    // startTime and endTime.
                    if (p.StartTime.GreaterThanOrEqual(periodStart) &&
                        p.StartTime.LessThanOrEqual(periodEnd))
                    {
                        occurrences.Add(new Occurrence(recurrable, p));
                    }
                }

                occurrences.Sort();
            }
            return(occurrences);
        }
예제 #2
0
        virtual public TimeZoneObservance?GetObservance(IDateTime dt)
        {
            if (Parent == null)
            {
                throw new Exception("Cannot call GetObservance() on a TimeZoneInfo whose Parent property is null.");
            }

            // Normalize date/time values within this time zone to a UTC value.
            DateTime normalizedDt = dt.Value;

            if (string.Equals(dt.TZID, TZID))
            {
                dt           = new iCalDateTime(OffsetTo.ToUTC(dt.Value));
                normalizedDt = OffsetTo.ToUTC(normalizedDt);
            }

            // Let's evaluate our time zone observances to find the
            // observance that applies to this date/time value.
            IEvaluator parentEval = Parent.GetService(typeof(IEvaluator)) as IEvaluator;

            if (parentEval != null)
            {
                // Evaluate the date/time in question.
                parentEval.Evaluate(Start, DateUtil.GetSimpleDateTimeData(Start), normalizedDt, true);
                foreach (IPeriod period in m_Evaluator.Periods)
                {
                    if (period.Contains(dt))
                    {
                        return(new TimeZoneObservance(period, this));
                    }
                }
            }
            return(null);
        }
예제 #3
0
        public void EvaluateToPreviousOccurrence(IDateTime completedDate, IDateTime currDt)
        {
            IDateTime beginningDate = completedDate.Copy <IDateTime>();

            if (Todo.RecurrenceRules != null)
            {
                foreach (IRecurrencePattern rrule in Todo.RecurrenceRules)
                {
                    DetermineStartingRecurrence(rrule, ref beginningDate);
                }
            }
            if (Todo.RecurrenceDates != null)
            {
                foreach (IPeriodList rdate in Todo.RecurrenceDates)
                {
                    DetermineStartingRecurrence(rdate, ref beginningDate);
                }
            }
            if (Todo.ExceptionRules != null)
            {
                foreach (IRecurrencePattern exrule in Todo.ExceptionRules)
                {
                    DetermineStartingRecurrence(exrule, ref beginningDate);
                }
            }
            if (Todo.ExceptionDates != null)
            {
                foreach (IPeriodList exdate in Todo.ExceptionDates)
                {
                    DetermineStartingRecurrence(exdate, ref beginningDate);
                }
            }

            Evaluate(Todo.Start, DateUtil.GetSimpleDateTimeData(beginningDate), DateUtil.GetSimpleDateTimeData(currDt).AddTicks(1), true);
        }
예제 #4
0
        static public IList <Occurrence> GetOccurrences(IRecurrable recurrable, IDateTime periodStart, IDateTime periodEnd, bool includeReferenceDateInResults)
        {
            List <Occurrence> occurrences = new List <Occurrence>();

            IEvaluator evaluator = recurrable.GetService(typeof(IEvaluator)) as IEvaluator;

            if (evaluator != null)
            {
                // Ensure the start time is associated with the object being queried
                IDateTime start = recurrable.Start.Copy <IDateTime>();
                start.AssociatedObject = recurrable as ICalendarObject;

                // Change the time zone of periodStart/periodEnd as needed
                // so they can be used during the evaluation process.
                periodStart = DateUtil.MatchTimeZone(start, periodStart);
                periodEnd   = DateUtil.MatchTimeZone(start, periodEnd);

                IList <IPeriod> periods = evaluator.Evaluate(
                    start,
                    DateUtil.GetSimpleDateTimeData(periodStart),
                    DateUtil.GetSimpleDateTimeData(periodEnd),
                    includeReferenceDateInResults);

                foreach (IPeriod p in periods)
                {
                    // Filter the resulting periods to only contain those
                    // that occur sometime between startTime and endTime.
                    // NOTE: fixes bug #3007244 - GetOccurences not returning long spanning all-day events
                    IDateTime endTime = p.EndTime ?? p.StartTime;
                    if (endTime.GreaterThan(periodStart) && p.StartTime.LessThanOrEqual(periodEnd))
                    {
                        occurrences.Add(new Occurrence(recurrable, p));
                    }
                }

                occurrences.Sort();
            }
            return(occurrences);
        }
예제 #5
0
        virtual public TimeZoneObservance?GetObservance(IDateTime dt)
        {
            if (Parent == null)
            {
                throw new Exception("Cannot call GetObservance() on a TimeZoneInfo whose Parent property is null.");
            }

            if (string.Equals(dt.TZID, TZID))
            {
                // Normalize date/time values within this time zone to a local value.
                DateTime normalizedDt = dt.Value;

                // Let's evaluate our time zone observances to find the
                // observance that applies to this date/time value.
                IEvaluator parentEval = Parent.GetService(typeof(IEvaluator)) as IEvaluator;
                if (parentEval != null)
                {
                    // Evaluate the date/time in question.
                    parentEval.Evaluate(Start, DateUtil.GetSimpleDateTimeData(Start), normalizedDt, true);

                    // NOTE: We avoid using period.Contains here, because we want to avoid
                    // doing an inadvertent time zone lookup with it.
                    var period = m_Evaluator
                                 .Periods
                                 .FirstOrDefault(p =>
                                                 p.StartTime.Value <= normalizedDt &&
                                                 p.EndTime.Value > normalizedDt
                                                 );

                    if (period != null)
                    {
                        return(new TimeZoneObservance(period, this));
                    }
                }
            }

            return(null);
        }
예제 #6
0
        /**
         * Returns a list of start dates in the specified period represented by this recur. This method includes a base date
         * argument, which indicates the start of the fist occurrence of this recurrence. The base date is used to inject
         * default values to return a set of dates in the correct format. For example, if the search start date (start) is
         * Wed, Mar 23, 12:19PM, but the recurrence is Mon - Fri, 9:00AM - 5:00PM, the start dates returned should all be at
         * 9:00AM, and not 12:19PM.
         */
        private HashSet <DateTime> GetDates(IDateTime seed, DateTime periodStart, DateTime periodEnd, int maxCount, IRecurrencePattern pattern, bool includeReferenceDateInResults)
        {
            var dates        = new HashSet <DateTime>();
            var originalDate = DateUtil.GetSimpleDateTimeData(seed);
            var seedCopy     = DateUtil.GetSimpleDateTimeData(seed);

            if (includeReferenceDateInResults)
            {
                dates.Add(seedCopy);
            }

            // optimize the start time for selecting candidates
            // (only applicable where a COUNT is not specified)
            if (pattern.Count == int.MinValue)
            {
                var incremented = seedCopy;
                IncrementDate(ref incremented, pattern, pattern.Interval);
                while (incremented < periodStart)
                {
                    seedCopy = incremented;
                    IncrementDate(ref incremented, pattern, pattern.Interval);
                }
            }

            var expandBehavior = RecurrenceUtil.GetExpandBehaviorList(pattern);

            var invalidCandidateCount     = 0;
            var noCandidateIncrementCount = 0;
            var candidate = DateTime.MinValue;

            while ((maxCount < 0) || (dates.Count < maxCount))
            {
                if (pattern.Until != DateTime.MinValue && candidate != DateTime.MinValue && candidate > pattern.Until)
                {
                    break;
                }

                if (periodEnd != null && candidate != DateTime.MinValue && candidate > periodEnd)
                {
                    break;
                }

                if (pattern.Count >= 1 && (dates.Count + invalidCandidateCount) >= pattern.Count)
                {
                    break;
                }

                var candidates = GetCandidates(seedCopy, pattern, expandBehavior);
                if (candidates.Count > 0)
                {
                    noCandidateIncrementCount = 0;

                    // sort candidates for identifying when UNTIL date is exceeded..
                    candidates.Sort();

                    for (var i = 0; i < candidates.Count; i++)
                    {
                        candidate = candidates[i];

                        // don't count candidates that occur before the original date..
                        if (candidate >= originalDate)
                        {
                            // candidates MAY occur before periodStart
                            // For example, FREQ=YEARLY;BYWEEKNO=1 could return dates
                            // from the previous year.
                            //
                            // candidates exclusive of periodEnd..
                            if (candidate >= periodEnd)
                            {
                                invalidCandidateCount++;
                            }
                            else if (pattern.Count >= 1 && (dates.Count + invalidCandidateCount) >= pattern.Count)
                            {
                                break;
                            }
                            else if (pattern.Until == DateTime.MinValue || candidate <= pattern.Until)
                            {
                                var utcCandidate = DateUtil.FromTimeZoneToTimeZone(candidate, DateUtil.GetZone(seed.TzId), DateTimeZone.Utc).ToDateTimeUtc();
                                if (!dates.Contains(candidate) && (pattern.Until == DateTime.MinValue || utcCandidate <= pattern.Until))
                                {
                                    dates.Add(candidate);
                                }
                            }
                        }
                    }
                }
                else
                {
                    noCandidateIncrementCount++;
                    if ((maxIncrementCount > 0) && (noCandidateIncrementCount > maxIncrementCount))
                    {
                        break;
                    }
                }

                IncrementDate(ref seedCopy, pattern, pattern.Interval);
            }

            // sort final list..
            return(dates);
        }
        /**
         * Returns the the next date of this recurrence given a seed date
         * and start date.  The seed date indicates the start of the fist
         * occurrence of this recurrence. The start date is the
         * starting date to search for the next recurrence.  Return null
         * if there is no occurrence date after start date.
         * @return the next date in the recurrence series after startDate
         * @param seed the start date of this Recurrence's first instance
         * @param startDate the date to start the search
         */
        private DateTime?GetNextDate(IDateTime referenceDate, DateTime periodStart, IRecurrencePattern pattern)
        {
            DateTime seedCopy = DateUtil.GetSimpleDateTimeData(referenceDate);

            // optimize the start time for selecting candidates
            // (only applicable where a COUNT is not specified)
            if (Pattern.Count == int.MinValue)
            {
                DateTime incremented = seedCopy;
                IncrementDate(ref incremented, pattern, pattern.Interval);
                while (incremented < periodStart)
                {
                    seedCopy = incremented;
                    IncrementDate(ref incremented, pattern, pattern.Interval);
                }
            }

            bool?[] expandBehaviors = RecurrenceUtil.GetExpandBehaviorList(pattern);

            int      invalidCandidateCount     = 0;
            int      noCandidateIncrementCount = 0;
            DateTime candidate = DateTime.MinValue;

            while (true)
            {
                if (pattern.Until != DateTime.MinValue && candidate != DateTime.MinValue && candidate > pattern.Until)
                {
                    break;
                }

                if (pattern.Count > 0 && invalidCandidateCount >= pattern.Count)
                {
                    break;
                }

                List <DateTime> candidates = GetCandidates(seedCopy, pattern, expandBehaviors);
                if (candidates.Count > 0)
                {
                    noCandidateIncrementCount = 0;

                    // sort candidates for identifying when UNTIL date is exceeded..
                    candidates.Sort();

                    for (int i = 0; i < candidates.Count; i++)
                    {
                        candidate = candidates[i];

                        // don't count candidates that occur before the seed date..
                        if (candidate >= seedCopy)
                        {
                            // Candidate must be after startDate because
                            // we want the NEXT occurrence
                            if (candidate <= periodStart)
                            {
                                invalidCandidateCount++;
                            }
                            else if (pattern.Count > 0 && invalidCandidateCount >= pattern.Count)
                            {
                                break;
                            }
                            else if (pattern.Until == DateTime.MinValue || candidate <= pattern.Until)
                            {
                                return(candidate);
                            }
                        }
                    }
                }
                else
                {
                    noCandidateIncrementCount++;
                    if ((maxIncrementCount > 0) && (noCandidateIncrementCount > maxIncrementCount))
                    {
                        break;
                    }
                }

                IncrementDate(ref seedCopy, pattern, pattern.Interval);
            }
            return(null);
        }
        /**
         * Returns a list of start dates in the specified period represented by this recur. This method includes a base date
         * argument, which indicates the start of the fist occurrence of this recurrence. The base date is used to inject
         * default values to return a set of dates in the correct format. For example, if the search start date (start) is
         * Wed, Mar 23, 12:19PM, but the recurrence is Mon - Fri, 9:00AM - 5:00PM, the start dates returned should all be at
         * 9:00AM, and not 12:19PM.
         */
        private IList <DateTime> GetDates(IDateTime seed, DateTime periodStart, DateTime periodEnd, int maxCount, IRecurrencePattern pattern, bool includeReferenceDateInResults)
        {
            SortedList <DateTime, DateTime> dates = new SortedList <DateTime, DateTime>();
            DateTime originalDate = DateUtil.GetSimpleDateTimeData(seed);
            DateTime seedCopy     = DateUtil.GetSimpleDateTimeData(seed);

            if (includeReferenceDateInResults)
            {
                dates.Add(seedCopy, seedCopy);
            }

            // If the interval is set to zero, or our count prevents us
            // from getting additional items, then return with the reference
            // date only.
            if (pattern.Interval == 0 ||
                (pattern.Count != int.MinValue && pattern.Count <= dates.Count))
            {
                return(dates.Values);
            }

            // optimize the start time for selecting candidates
            // (only applicable where a COUNT is not specified)
            if (pattern.Count == int.MinValue)
            {
                DateTime incremented = seedCopy;
                // FIXME: we can more aggresively increment here when
                // the difference between dates is greater.
                IncrementDate(ref incremented, pattern, pattern.Interval);
                while (incremented < periodStart)
                {
                    seedCopy = incremented;
                    IncrementDate(ref incremented, pattern, pattern.Interval);
                }
            }

            bool?[] expandBehavior = RecurrenceUtil.GetExpandBehaviorList(pattern);

            int      invalidCandidateCount     = 0;
            int      noCandidateIncrementCount = 0;
            DateTime candidate = DateTime.MinValue;

            while ((maxCount < 0) || (dates.Count < maxCount))
            {
                if (pattern.Until != DateTime.MinValue && candidate != DateTime.MinValue && candidate > pattern.Until)
                {
                    break;
                }

                if (periodEnd != null && candidate != DateTime.MinValue && candidate > periodEnd)
                {
                    break;
                }

                if (pattern.Count >= 1 && (dates.Count + invalidCandidateCount) >= pattern.Count)
                {
                    break;
                }

                List <DateTime> candidates = GetCandidates(seedCopy, pattern, expandBehavior);
                if (candidates.Count > 0)
                {
                    noCandidateIncrementCount = 0;

                    // sort candidates for identifying when UNTIL date is exceeded..
                    candidates.Sort();

                    for (int i = 0; i < candidates.Count; i++)
                    {
                        candidate = candidates[i];

                        // don't count candidates that occur before the original date..
                        if (candidate >= originalDate)
                        {
                            // candidates MAY occur before periodStart
                            // For example, FREQ=YEARLY;BYWEEKNO=1 could return dates
                            // from the previous year.
                            //
                            // candidates exclusive of periodEnd..
                            if (candidate >= periodEnd)
                            {
                                invalidCandidateCount++;
                            }
                            else if (pattern.Count >= 1 && (dates.Count + invalidCandidateCount) >= pattern.Count)
                            {
                                break;
                            }
                            else if (pattern.Until == DateTime.MinValue || candidate <= pattern.Until)
                            {
                                if (!dates.ContainsKey(candidate))
                                {
                                    dates.Add(candidate, candidate);
                                }
                            }
                        }
                    }
                }
                else
                {
                    noCandidateIncrementCount++;
                    if ((maxIncrementCount > 0) && (noCandidateIncrementCount > maxIncrementCount))
                    {
                        break;
                    }
                }

                IncrementDate(ref seedCopy, pattern, pattern.Interval);
            }

            return(dates.Values);
        }