Пример #1
0
        public static iCalDateTime operator +(iCalDateTime left, TimeSpan right)
        {
            iCalDateTime copy = left.Copy();

            copy.Value += right;
            return(copy);
        }
Пример #2
0
        public iCalDateTime AddMonths(int months)
        {
            iCalDateTime dt = Copy();

            dt.Value = Value.AddMonths(months);
            return(dt);
        }
Пример #3
0
        public iCalDateTime AddYears(int years)
        {
            iCalDateTime dt = Copy();

            dt.Value = Value.AddYears(years);
            return(dt);
        }
Пример #4
0
        public iCalDateTime AddHours(int hours)
        {
            iCalDateTime dt = Copy();

            dt.Value = Value.AddHours(hours);
            return(dt);
        }
Пример #5
0
        public iCalDateTime AddDays(int days)
        {
            iCalDateTime dt = Copy();

            dt.Value = Value.AddDays(days);
            return(dt);
        }
Пример #6
0
        public iCalDateTime AddMinutes(int minutes)
        {
            iCalDateTime dt = Copy();

            dt.Value = Value.AddMinutes(minutes);
            return(dt);
        }
Пример #7
0
        public List <Period> Evaluate(iCalDateTime StartDate, iCalDateTime FromDate, iCalDateTime EndDate)
        {
            List <Period> periods = new List <Period>();

            if (StartDate > FromDate)
            {
                FromDate = StartDate;
            }

            if (EndDate < FromDate ||
                FromDate > EndDate)
            {
                return(periods);
            }

            foreach (Period p in Periods)
            {
                if (!periods.Contains(p))
                {
                    periods.Add(p);
                }
            }

            return(periods);
        }
Пример #8
0
        public iCalDateTime AddSeconds(int seconds)
        {
            iCalDateTime dt = Copy();

            dt.Value = Value.AddSeconds(seconds);
            return(dt);
        }
Пример #9
0
        public void RRULE1()
        {
            iCalendar iCal = iCalendar.LoadFromFile(@"Calendars\Recurrence\RRULE1.ics");
            Program.TestCal(iCal);
            Event evt = iCal.Events[0];
            List<Occurrence> occurrences = evt.GetOccurrences(
                new iCalDateTime(2006, 1, 1, tzid, iCal),
                new iCalDateTime(2011, 1, 1, tzid, iCal));

            iCalDateTime dt = new iCalDateTime(2006, 1, 1, 8, 30, 0, tzid, iCal);
            int i = 0;

            while (dt.Year < 2011)
            {
                if ((dt > evt.Start) &&
                    (dt.Year % 2 == 1) && // Every-other year from 2005
                    (dt.Month == 1) &&
                    (dt.DayOfWeek == DayOfWeek.Sunday))
                {
                    iCalDateTime dt1 = dt.AddHours(1);
                    Assert.AreEqual(dt, occurrences[i].Period.StartTime, "Event should occur at " + dt);
                    Assert.AreEqual(dt1, occurrences[i + 1].Period.StartTime, "Event should occur at " + dt);
                    i += 2;
                }                

                dt = dt.AddDays(1);
            }
        }
Пример #10
0
        public iCalDateTime AddMilliseconds(int milliseconds)
        {
            iCalDateTime dt = Copy();

            dt.Value = Value.AddMilliseconds(milliseconds);
            return(dt);
        }
Пример #11
0
 public Period(iCalDateTime start, TimeSpan duration)
     : this()
 {
     StartTime = start.Copy();
     if (duration != TimeSpan.MinValue)
     {
         Duration = new Duration(duration);
         EndTime = start + duration;
     }            
 }
Пример #12
0
 public Period(iCalDateTime start, iCalDateTime end)
     : this()
 {
     StartTime = start.Copy();
     if (end != null)
     {
         EndTime = end.Copy();
         Duration = new Duration(end.Value - start.Value);
     }            
 }
Пример #13
0
 public Period(iCalDateTime start, TimeSpan duration)
     : this()
 {
     StartTime = start.Copy();
     if (duration != TimeSpan.MinValue)
     {
         Duration = new Duration(duration);
         EndTime  = start + duration;
     }
 }
Пример #14
0
 public Period(iCalDateTime start, iCalDateTime end)
     : this()
 {
     StartTime = start.Copy();
     if (end != null)
     {
         EndTime  = end.Copy();
         Duration = new Duration(end.Value - start.Value);
     }
 }
Пример #15
0
        public static long DateDiff(FrequencyType frequency, iCalDateTime dt1, iCalDateTime dt2, DayOfWeek firstDayOfWeek)
        {
            if (frequency == FrequencyType.Yearly)
            {
                return(dt2.Year - dt1.Year);
            }

            if (frequency == FrequencyType.Monthly)
            {
                return((dt2.Month - dt1.Month) + (12 * (dt2.Year - dt1.Year)));
            }

            if (frequency == FrequencyType.Weekly)
            {
                // Get the week of year of the time frame we want to calculate
                int firstEvalWeek = _Calendar.GetWeekOfYear(dt2.Value, System.Globalization.CalendarWeekRule.FirstFourDayWeek, firstDayOfWeek);

                // Count backwards in years, calculating how many weeks' difference we have between
                // first and second dates
                iCalDateTime evalDate = dt2.Copy();
                while (evalDate.Year > dt1.Year)
                {
                    firstEvalWeek += _Calendar.GetWeekOfYear(new DateTime(evalDate.Year - 1, 12, 31), System.Globalization.CalendarWeekRule.FirstFourDayWeek, firstDayOfWeek);
                    evalDate       = evalDate.AddYears(-1);
                }

                // Determine the difference, in weeks, between the start date and the evaluation period.
                int startWeek = _Calendar.GetWeekOfYear(dt1.Value, System.Globalization.CalendarWeekRule.FirstFourDayWeek, firstDayOfWeek);
                return(firstEvalWeek - startWeek);
            }

            TimeSpan ts = dt2 - dt1;

            if (frequency == FrequencyType.Daily)
            {
                return(Round(ts.TotalDays));
            }

            if (frequency == FrequencyType.Hourly)
            {
                return(Round(ts.TotalHours));
            }

            if (frequency == FrequencyType.Minutely)
            {
                return(Round(ts.TotalMinutes));
            }

            if (frequency == FrequencyType.Secondly)
            {
                return(Round(ts.TotalSeconds));
            }

            return(0);
        }
Пример #16
0
 virtual public void MergeWith(iCalDateTime dt)
 {
     if (iCalendar == null)
     {
         iCalendar = dt.iCalendar;
     }
     if (TZID == null)
     {
         TZID = dt.TZID;
     }
     IsUniversalTime = dt.IsUniversalTime;
 }
Пример #17
0
        public override System.Collections.Generic.List<Period> Evaluate(iCalDateTime FromDate, iCalDateTime ToDate)
        {
            if (Start != null)
            {
                Period p = new Period(Start);
                if (!Periods.Contains(p))
                    Periods.Add(p);

                return base.Evaluate(FromDate, ToDate);
            }
            return new System.Collections.Generic.List<Period>();
        }
Пример #18
0
        public override bool TryParse(string value, ref object obj)
        {
            string[] values = value.Split('T');

            if (obj == null)
            {
                obj = new iCalDateTime();
            }
            iCalDateTime dt = (iCalDateTime)obj;

            Match match = Regex.Match(value, @"^((\d{4})(\d{2})(\d{2}))?T?((\d{2})(\d{2})(\d{2})(Z)?)?$", RegexOptions.IgnoreCase);

            if (!match.Success)
            {
                return(false);
            }
            else
            {
                DateTime now = DateTime.Now;

                int year   = now.Year;
                int month  = now.Month;
                int date   = now.Day;
                int hour   = 0;
                int minute = 0;
                int second = 0;

                if (match.Groups[1].Success)
                {
                    dt.HasDate = true;
                    year       = Convert.ToInt32(match.Groups[2].Value);
                    month      = Convert.ToInt32(match.Groups[3].Value);
                    date       = Convert.ToInt32(match.Groups[4].Value);
                }
                if (match.Groups[5].Success)
                {
                    dt.HasTime = true;
                    hour       = Convert.ToInt32(match.Groups[6].Value);
                    minute     = Convert.ToInt32(match.Groups[7].Value);
                    second     = Convert.ToInt32(match.Groups[8].Value);
                }

                if (match.Groups[9].Success)
                {
                    dt.IsUniversalTime = true;
                }
                DateTime setDateTime = new DateTime(year, month, date, hour, minute, second, DateTimeKind.Utc);

                dt.Value = setDateTime;
            }
            return(true);
        }
Пример #19
0
        public iCalDateTimeUTCSerializer(iCalDateTime dt)
            : base(dt)
        {
            // Make a copy of the iCalDateTime object, so we don't alter
            // the original
            DateTime = dt.Copy();

            // Set the iCalDateTime object to UTC time
            DateTime = DateTime.UTC;

            // Ensure time is serialized
            DateTime.HasTime = true;                    
        }
Пример #20
0
 public override void CopyFrom(object obj)
 {
     base.CopyFrom(obj);
     if (obj is iCalDateTime)
     {
         iCalDateTime dt = (iCalDateTime)obj;
         this.Value           = dt.Value;
         this.HasDate         = dt.HasDate;
         this.HasTime         = dt.HasTime;
         this.TZID            = dt.TZID;
         this.iCalendar       = dt.iCalendar;
         this.IsUniversalTime = dt.IsUniversalTime;
     }
     base.CopyFrom(obj);
 }
Пример #21
0
        public static iCalDateTime AddFrequency(FrequencyType frequency, iCalDateTime dt, int interval)
        {
            switch (frequency)
            {
            case FrequencyType.Yearly: return(dt.AddYears(interval));

            case FrequencyType.Monthly: return(dt.AddMonths(interval));

            case FrequencyType.Weekly: return(dt.AddDays(interval * 7));

            case FrequencyType.Daily: return(dt.AddDays(interval));

            case FrequencyType.Hourly: return(dt.AddHours(interval));

            case FrequencyType.Minutely: return(dt.AddMinutes(interval));

            case FrequencyType.Secondly: return(dt.AddSeconds(interval));

            default: return(dt);
            }
        }
Пример #22
0
        public override bool TryParse(string value, ref object obj)
        {
            string[] values = value.Split(',');
            foreach (string v in values)
            {
                object dt = new iCalDateTime();
                object p  = new Period();

                //
                // Set the iCalendar for each iCalDateTime object here,
                // so that any time zones applied to these objects will be
                // handled correctly.
                // NOTE: fixes RRULE30 eval, where EXDATE references a
                // DATE-TIME without a time zone; therefore, the time zone
                // is implied from DTSTART, and would fail to do a proper
                // time zone lookup, because it wasn't assigned an iCalendar
                // object.
                //
                if (((iCalDateTime)dt).TryParse(v, ref dt))
                {
                    ((iCalDateTime)dt).iCalendar = iCalendar;
                    ((iCalDateTime)dt).TZID      = TZID;
                    Periods.Add(new Period((iCalDateTime)dt));
                }
                else if (((Period)p).TryParse(v, ref p))
                {
                    ((Period)p).StartTime.iCalendar = ((Period)p).EndTime.iCalendar = iCalendar;
                    ((Period)p).StartTime.TZID      = ((Period)p).EndTime.TZID = TZID;
                    Periods.Add((Period)p);
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #23
0
        public void TestAlarm(string Calendar, List<iCalDateTime> Dates, iCalDateTime Start, iCalDateTime End)
        {
            iCalendar iCal = iCalendar.LoadFromFile(@"Calendars\Alarm\" + Calendar);
            Program.TestCal(iCal);
            Event evt = iCal.Events[0];

            Start.iCalendar = iCal;
            Start.TZID = tzid;
            End.iCalendar = iCal;
            End.TZID = tzid;

            for (int i = 0; i < Dates.Count; i++)
            {
                Dates[i].TZID = tzid;
                Dates[i].iCalendar = iCal;
            }

            // Poll all alarms that occurred between Start and End
            List<AlarmOccurrence> alarms = evt.PollAlarms(Start, End);

            foreach (AlarmOccurrence alarm in alarms)
                Assert.IsTrue(Dates.Contains(alarm.DateTime), "Alarm triggers at " + alarm.Period.StartTime + ", but it should not.");
            Assert.IsTrue(Dates.Count == alarms.Count, "There were " + alarms.Count + " alarm occurrences; there should have been " + Dates.Count + ".");
        }
Пример #24
0
 public Period(iCalDateTime occurs) : this(occurs, null)
 {
 }
Пример #25
0
        /// <summary>
        /// Retrieves the iCalTimeZoneInfo object that contains information
        /// about the TimeZone, with the name of the current timezone,
        /// offset from UTC, etc.
        /// </summary>
        /// <param name="dt">The iCalDateTime object for which to retrieve the iCalTimeZoneInfo.</param>
        /// <returns>A TimeZoneInfo object for the specified iCalDateTime</returns>
        public iCalTimeZoneInfo GetTimeZoneInfo(iCalDateTime dt)
        {
            iCalTimeZoneInfo tzi = null;

            TimeSpan mostRecent = TimeSpan.MaxValue;
            foreach (iCalTimeZoneInfo curr in TimeZoneInfos)
            {
                DateTime Start = new DateTime(dt.Year - 1, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                DateTime End = new DateTime(dt.Year + 1, 1, 1, 0, 0, 0, DateTimeKind.Utc);

                DateTime dtUTC = dt.Value;
                dtUTC = DateTime.SpecifyKind(dtUTC, DateTimeKind.Utc);

                // Time zones must include an effective start date/time.
                if (curr.Start == null)
                    continue;

                // Make a copy of the current start value
                iCalDateTime currStart = curr.Start.Copy();
                if (curr.TZOffsetTo != null)
                {
                    int mult = curr.TZOffsetTo.Positive ? -1 : 1;
                    dtUTC = dtUTC.AddHours(curr.TZOffsetTo.Hours * mult);
                    dtUTC = dtUTC.AddMinutes(curr.TZOffsetTo.Minutes * mult);
                    dtUTC = dtUTC.AddSeconds(curr.TZOffsetTo.Seconds * mult);
                    // Offset the current start value to match our offset time...
                    currStart = currStart.AddHours(curr.TZOffsetTo.Hours * mult);
                    currStart = currStart.AddMinutes(curr.TZOffsetTo.Minutes * mult);
                    currStart = currStart.AddSeconds(curr.TZOffsetTo.Seconds * mult);
                }

                // Determine the UTC occurrences of the Time Zone changes                
                if (curr.EvalStart == null ||
                    curr.EvalEnd == null ||
                    dtUTC < curr.EvalStart.Value ||
                    dtUTC > curr.EvalEnd.Value)
                    curr.Evaluate(Start, End);

                // If the date is past the last allowed date, then don't consider it!
                // NOTE: if this time zone ends as another begins, then there can
                // be up to a 1 year period of "unhandled" time unless we add a year
                // to the "Until" date.  For example, one time period "ends" in Oct. 2006
                // (the last occurrence), and the next begins in Apr. 2007.  If we didn't
                // add 1 year to the "Until" time, the 6 month period between Oct. 2006
                // and Apr. 2007 would be unhandled.

                // FIXME: this thinking may be flawed. We should try to find some other way...
                //
                //if (curr.Until != null &&
                //    dtUTC > curr.Until.AddYears(1))
                //    continue;

                foreach (Period p in curr.Periods)
                {
                    TimeSpan currentSpan = dtUTC - p.StartTime;
                    if (currentSpan.Ticks >= 0 &&
                        currentSpan.Ticks < mostRecent.Ticks)
                    {
                        mostRecent = currentSpan;
                        tzi = curr;
                    }
                }
            }

            return tzi;
        }
Пример #26
0
        public void RECURPARSE1()
        {
            iCalendar iCal = new iCalendar();

            Event evt = iCal.Create<Event>();
            evt.Summary = "Test event";
            evt.Start = new iCalDateTime(2006, 10, 1, 9, 0, 0);
            evt.Duration = new TimeSpan(1, 0, 0);
            evt.AddRecurrencePattern(new RecurrencePattern("Every 3rd month on the last tuesday and wednesday"));

            List<Occurrence> occurrences = evt.GetOccurrences(
                new iCalDateTime(2006, 10, 1), 
                new iCalDateTime(2007, 4, 30));

            iCalDateTime[] DateTimes = new iCalDateTime[]
            {
                new iCalDateTime(2006, 10, 1, 9, 0, 0),
                new iCalDateTime(2006, 10, 25, 9, 0, 0),
                new iCalDateTime(2006, 10, 31, 9, 0, 0),
                new iCalDateTime(2007, 1, 30, 9, 0, 0),
                new iCalDateTime(2007, 1, 31, 9, 0, 0),
                new iCalDateTime(2007, 4, 24, 9, 0, 0),
                new iCalDateTime(2007, 4, 25, 9, 0, 0)
            };

            for (int i = 0; i < DateTimes.Length; i++)
                Assert.AreEqual(DateTimes[i], occurrences[i].Period.StartTime, "Event should occur on " + DateTimes[i]);

            Assert.AreEqual(
                DateTimes.Length,
                occurrences.Count,
                "There should be exactly " + DateTimes.Length +
                " occurrences; there were " + occurrences.Count);
        }
Пример #27
0
        public void RRULE6()
        {
            iCalendar iCal = iCalendar.LoadFromFile(@"Calendars\Recurrence\RRULE6.ics");
            Program.TestCal(iCal);
            Event evt = iCal.Events[0];

            List<Occurrence> occurrences = evt.GetOccurrences(
                new iCalDateTime(1998, 1, 1, tzid, iCal), 
                new iCalDateTime(2000, 12, 31, tzid, iCal));

            iCalDateTime dt = new iCalDateTime(1998, 1, 1, 9, 0, 0, tzid, iCal);
            int i = 0;
            while (dt.Year < 2001)
            {
                if (dt >= evt.Start &&
                    dt.Month == 1 &&
                    dt <= new iCalDateTime(2000, 1, 31, 9, 0, 0, tzid, iCal))
                {
                    Assert.AreEqual(dt, occurrences[i].Period.StartTime, "Event should occur at " + dt);
                    i++;
                }

                dt = dt.AddDays(1);
            }
        }
Пример #28
0
 /// <summary>
 /// [Deprecated]: Use IsValidDate() instead.
 /// </summary>
 public bool CheckValidDate(iCalDateTime dt) { return IsValidDate(dt); }
Пример #29
0
        public void RRULE13()
        {
            iCalendar iCal = iCalendar.LoadFromFile(@"Calendars\Recurrence\RRULE13.ics");
            Program.TestCal(iCal);
            Event evt = iCal.Events[0];

            List<Occurrence> occurrences = evt.GetOccurrences(
                new iCalDateTime(1996, 1, 1, tzid, iCal), 
                new iCalDateTime(1999, 1, 1, tzid, iCal));

            iCalDateTime[] DateTimes = new iCalDateTime[]
            {
                new iCalDateTime(1997, 9, 2, 9, 0, 0, tzid, iCal),
                new iCalDateTime(1997, 9, 4, 9, 0, 0, tzid, iCal),
                new iCalDateTime(1997, 9, 16, 9, 0, 0, tzid, iCal),
                new iCalDateTime(1997, 9, 18, 9, 0, 0, tzid, iCal),
                new iCalDateTime(1997, 9, 30, 9, 0, 0, tzid, iCal),
                new iCalDateTime(1997, 10, 2, 9, 0, 0, tzid, iCal),
                new iCalDateTime(1997, 10, 14, 9, 0, 0, tzid, iCal),
                new iCalDateTime(1997, 10, 16, 9, 0, 0, tzid, iCal)
            };

            for (int i = 0; i < DateTimes.Length; i++)
                Assert.AreEqual(DateTimes[i], occurrences[i].Period.StartTime, "Event should occur on " + DateTimes[i]);

            Assert.AreEqual(
                DateTimes.Length,
                occurrences.Count,
                "There should be exactly " + DateTimes.Length + " occurrences; there were " + occurrences.Count);
        }
Пример #30
0
 public iCalDateTime(iCalDateTime value) : this()
 {
     CopyFrom(value);
 }
Пример #31
0
            public TimeCalculation(iCalDateTime StartDate, iCalDateTime EndDate, RecurrencePattern Recur)
            {
                this.StartDate = StartDate;
                this.EndDate = EndDate;
                this.Recur = Recur;

                Year = StartDate.Value.Year;
                Month = StartDate.Value.Month;
                Day = StartDate.Value.Day;
                Hour = StartDate.Value.Hour;
                Minute = StartDate.Value.Minute;
                Second = StartDate.Value.Second;

                YearDays = new List<int>(Recur.ByYearDay);
                ByDays = new List<DaySpecifier>(Recur.ByDay);
                Months = new List<int>(Recur.ByMonth);
                Days = new List<int>(Recur.ByMonthDay);
                Hours = new List<int>(Recur.ByHour);
                Minutes = new List<int>(Recur.ByMinute);
                Seconds = new List<int>(Recur.BySecond);
                DateTimes = new List<iCalDateTime>();

                // Only check what months and days are possible for
                // the week's period of time we're evaluating
                // NOTE: fixes RRULE10 evaluation                
                if (Recur.Frequency == FrequencyType.Weekly)
                {                    
                    if (Months.Count == 0)
                    {
                        Months.Add(StartDate.Value.Month);
                        if (StartDate.Value.Month != EndDate.Value.Month)
                            Months.Add(EndDate.Value.Month);
                    }
                    if (Days.Count == 0)
                    {
                        DateTime dt = StartDate.Value;
                        while (dt < EndDate.Value)
                        {
                            Days.Add(dt.Day);
                            dt = dt.AddDays(1);
                        }
                        Days.Add(EndDate.Value.Day);
                    }
                }
                else
                {
                    if (Months.Count == 0) Months.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });                                
                    if (Days.Count == 0) Days.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 });
                }
            }
Пример #32
0
        public void RECURPARSE6()
        {
            iCalendar iCal = new iCalendar();

            Event evt = iCal.Create<Event>();
            evt.Summary = "Test event";
            evt.Start = new iCalDateTime(2006, 1, 1, 9, 0, 0);
            evt.Duration = new TimeSpan(1, 0, 0);
            evt.AddRecurrencePattern(new RecurrencePattern("Every month on the first sunday, at 5:00PM, and at 7:00PM"));

            List<Occurrence> occurrences = evt.GetOccurrences(
                new iCalDateTime(2006, 1, 1), 
                new iCalDateTime(2006, 3, 31));

            iCalDateTime[] DateTimes = new iCalDateTime[]
            {
                new iCalDateTime(2006, 1, 1, 9, 0, 0),
                new iCalDateTime(2006, 1, 1, 17, 0, 0),
                new iCalDateTime(2006, 1, 1, 19, 0, 0),
                new iCalDateTime(2006, 2, 5, 17, 0, 0),
                new iCalDateTime(2006, 2, 5, 19, 0, 0),
                new iCalDateTime(2006, 3, 5, 17, 0, 0),
                new iCalDateTime(2006, 3, 5, 19, 0, 0)
            };

            for (int i = 0; i < DateTimes.Length; i++)
                Assert.AreEqual(DateTimes[i], occurrences[i].Period.StartTime, "Event should occur on " + DateTimes[i]);

            Assert.AreEqual(
                DateTimes.Length,
                occurrences.Count,
                "There should be exactly " + DateTimes.Length +
                " occurrences; there were " + occurrences.Count);
        }
Пример #33
0
        protected List<iCalDateTime> GetOccurrences(iCalDateTime StartDate, iCalDateTime FromDate, iCalDateTime ToDate, int Count)
        {
            List<iCalDateTime> DateTimes = new List<iCalDateTime>();

            // If the Recur is restricted by COUNT, we need to evaluate just
            // after any static occurrences so it's correctly restricted to a
            // certain number. NOTE: fixes bug #13 and bug #16
            if (Count > 0)
            {
                FromDate = StartDate;
                foreach (iCalDateTime dt in StaticOccurrences)
                {
                    if (FromDate < dt)
                        FromDate = dt;
                }
            }

            // Handle "UNTIL" values that are date-only. If we didn't change values here, "UNTIL" would
            // exclude the day it specifies, instead of the inclusive behaviour it should exhibit.
            if (Until != null && !Until.HasTime)
                Until.Value = new DateTime(Until.Year, Until.Month, Until.Day, 23, 59, 59, Until.Value.Kind);

            // Ignore recurrences that occur outside our time frame we're looking at
            if ((Until != null && FromDate > Until) ||
                ToDate < StartDate)
                return DateTimes;

            // Narrow down our time range further to avoid over-processing
            if (Until != null && Until < ToDate)
                ToDate = Until;
            if (StartDate > FromDate)
                FromDate = StartDate;

            // If the interval is greater than 1, then we need to ensure that the StartDate occurs in one of the
            // "active" days/weeks/months/years/etc. to ensure that we properly "step" through the interval.
            // NOTE: Fixes bug #1741093 - WEEKLY frequency eval behaves strangely
            {
                long difference = DateUtils.DateDiff(Frequency, StartDate, FromDate, Wkst);
                while (difference % Interval > 0)
                {
                    FromDate = DateUtils.AddFrequency(Frequency, FromDate, -1);
                    difference--;
                }                
            }

            // If the start date has no time, then our "From" date should not either 
            // NOTE: Fixes bug #1876582 - All-day holidays are sometimes giving incorrect times
            if (!StartDate.HasTime)
            {
                FromDate = new iCalDateTime(FromDate.Year, FromDate.Month, FromDate.Day, StartDate.Hour, StartDate.Minute, StartDate.Second);
                FromDate.IsUniversalTime = StartDate.IsUniversalTime;
                FromDate.HasTime = false;
            }

            while (
                FromDate <= ToDate &&
                (
                    Count == int.MinValue ||
                    DateTimes.Count <= Count)
                )
            {
                // Retrieve occurrences that occur on our interval period
                if (BySetPos.Count == 0 && IsValidDate(FromDate) && !DateTimes.Contains(FromDate.Value))
                    DateTimes.Add(FromDate.Copy());

                // Retrieve "extra" occurrences that happen within our interval period
                if (Frequency > FrequencyType.Secondly)
                {
                    foreach (iCalDateTime dt in GetExtraOccurrences(FromDate, ToDate))
                    {
                        // Don't add duplicates
                        if (!DateTimes.Contains(dt))
                            DateTimes.Add(dt.Copy());
                    }
                }

                IncrementDate(ref FromDate);
            }

            return DateTimes;
        }
Пример #34
0
 public void Remove(iCalDateTime dt)
 {
     Periods.Remove(new Period(dt));
 }
Пример #35
0
        protected List<iCalDateTime> CalculateChildOccurrences(iCalDateTime StartDate, iCalDateTime EndDate)
        {
            TimeCalculation TC = new TimeCalculation(StartDate, EndDate, this);                        
            switch (Frequency)
            {
                case FrequencyType.Yearly:
                    FillYearDays(TC);
                    FillByDay(TC);
                    FillMonths(TC);
                    break;
                case FrequencyType.Weekly: 
                    // Weeks can span across months, so we must
                    // fill months (Note: fixes RRULE10 eval)                    
                    FillMonths(TC);
                    break;
                case FrequencyType.Monthly:
                    FillDays(TC);
                    FillByDay(TC);
                    break;
                case FrequencyType.Daily:
                    FillHours(TC);
                    break;
                case FrequencyType.Hourly:
                    FillMinutes(TC);
                    break;
                case FrequencyType.Minutely:
                    FillSeconds(TC);
                    break;
                default:
                    throw new NotSupportedException("CalculateChildOccurrences() is not supported for a frequency of " + Frequency.ToString());                    
            }

            // Apply the BYSETPOS to the list of child occurrences
            // We do this before the dates are filtered by Start and End date
            // so that the BYSETPOS calculates correctly.
            // NOTE: fixes RRULE33 eval
            if (BySetPos.Count != 0)
            {
                List<iCalDateTime> newDateTimes = new List<iCalDateTime>();
                foreach (int pos in BySetPos)
                {
                    if (Math.Abs(pos) <= TC.DateTimes.Count)
                    {
                        if (pos > 0)
                            newDateTimes.Add(TC.DateTimes[pos - 1]);
                        else if (pos < 0)
                            newDateTimes.Add(TC.DateTimes[TC.DateTimes.Count + pos]);
                    }
                }

                TC.DateTimes = newDateTimes;
            }

            // Filter dates by Start and End date
            for (int i = TC.DateTimes.Count - 1; i >= 0; i--)
            {
                if ((iCalDateTime)TC.DateTimes[i] < StartDate ||
                    (iCalDateTime)TC.DateTimes[i] > EndDate)
                    TC.DateTimes.RemoveAt(i);
            }

            return TC.DateTimes;
        }
Пример #36
0
        public void IncrementDate(ref iCalDateTime dt, int Interval)
        {
            iCalDateTime old = dt.Copy();
            switch (Frequency)
            {
                case FrequencyType.Secondly: dt = old.AddSeconds(Interval); break;
                case FrequencyType.Minutely: dt = old.AddMinutes(Interval); break;
                case FrequencyType.Hourly: dt = old.AddHours(Interval); break;
                case FrequencyType.Daily: dt = old.AddDays(Interval); break;
                case FrequencyType.Weekly:
                    // How the week increments depends on the WKST indicated (defaults to Monday)
                    // So, basically, we determine the week of year using the necessary rules,
                    // and we increment the day until the week number matches our "goal" week number.
                    // So, if the current week number is 36, and our Interval is 2, then our goal
                    // week number is 38.
                    // NOTE: fixes RRULE12 eval.
                    int current = _Calendar.GetWeekOfYear(old.Value, System.Globalization.CalendarWeekRule.FirstFourDayWeek, Wkst),
                        lastLastYear = _Calendar.GetWeekOfYear(new DateTime(old.Year-1, 12, 31, 0, 0, 0, DateTimeKind.Local), System.Globalization.CalendarWeekRule.FirstFourDayWeek, Wkst),
                        last = _Calendar.GetWeekOfYear(new DateTime(old.Year, 12, 31, 0, 0, 0, DateTimeKind.Local), System.Globalization.CalendarWeekRule.FirstFourDayWeek, Wkst),
                        goal = current + Interval;

                    // If the goal week is greater than the last week of the year, wrap it!
                    if (goal > last)
                        goal = goal - last;
                    else if (goal <= 0)
                        goal = lastLastYear + goal;

                    int interval = Interval > 0 ? 1 : -1;
                    while (current != goal)
                    {
                        old = old.AddDays(interval);
                        current = _Calendar.GetWeekOfYear(old.Value, System.Globalization.CalendarWeekRule.FirstFourDayWeek, Wkst);
                    }

                    dt = old;
                    break;
                case FrequencyType.Monthly: dt = old.AddDays(-old.Day + 1).AddMonths(Interval); break;
                case FrequencyType.Yearly: dt = old.AddDays(-old.DayOfYear + 1).AddYears(Interval); break;
                default: throw new Exception("FrequencyType.NONE cannot be evaluated. Please specify a FrequencyType before evaluating the recurrence.");
            }
        }
Пример #37
0
 public void Add(iCalDateTime dt)
 {
     Periods.Add(new Period(dt));
 }
Пример #38
0
        public bool CheckValidDate(RecurrencePattern r, iCalDateTime Date)
        {
            bool valid = false;

            if (this.DayOfWeek == Date.Value.DayOfWeek)
            {
                valid = true;
            }

            if (valid && this.Num != int.MinValue)
            {
                int mult   = (this.Num < 0) ? -1 : 1;
                int offset = (this.Num < 0) ? 1 : 0;
                int abs    = Math.Abs(this.Num);

                switch (r.Frequency)
                {
                case FrequencyType.Monthly:
                {
                    DateTime mondt = new DateTime(Date.Value.Year, Date.Value.Month, 1, Date.Value.Hour, Date.Value.Minute, Date.Value.Second, Date.Value.Kind);
                    mondt = DateTime.SpecifyKind(mondt, Date.Value.Kind);
                    if (offset > 0)
                    {
                        mondt = mondt.AddMonths(1).AddDays(-1);
                    }

                    while (mondt.DayOfWeek != this.DayOfWeek)
                    {
                        mondt = mondt.AddDays(mult);
                    }

                    for (int i = 1; i < abs; i++)
                    {
                        mondt = mondt.AddDays(7 * mult);
                    }

                    if (Date.Value.Date != mondt.Date)
                    {
                        valid = false;
                    }
                } break;

                case FrequencyType.Yearly:
                {
                    // If BYMONTH is specified, then offset our tests
                    // by those months; otherwise, begin with Jan. 1st.
                    // NOTE: fixes USHolidays.ics eval
                    List <int> months = new List <int>();
                    if (r.ByMonth.Count == 0)
                    {
                        months.Add(1);
                    }
                    else
                    {
                        months = r.ByMonth;
                    }

                    bool found = false;
                    foreach (int month in months)
                    {
                        DateTime yeardt = new DateTime(Date.Value.Year, month, 1, Date.Value.Hour, Date.Value.Minute, Date.Value.Second, Date.Value.Kind);
                        yeardt = DateTime.SpecifyKind(yeardt, Date.Value.Kind);
                        if (offset > 0)
                        {
                            // Start at end of year, or end of month if BYMONTH is specified
                            if (r.ByMonth.Count == 0)
                            {
                                yeardt = yeardt.AddYears(1).AddDays(-1);
                            }
                            else
                            {
                                yeardt = yeardt.AddMonths(1).AddDays(-1);
                            }
                        }

                        while (yeardt.DayOfWeek != this.DayOfWeek)
                        {
                            yeardt = yeardt.AddDays(mult);
                        }

                        for (int i = 1; i < abs; i++)
                        {
                            yeardt = yeardt.AddDays(7 * mult);
                        }

                        if (Date.Value == yeardt)
                        {
                            found = true;
                        }
                    }

                    if (!found)
                    {
                        valid = false;
                    }
                } break;

                // Ignore other frequencies
                default: break;
                }
            }
            return(valid);
        }
Пример #39
0
        public void RRULE18()
        {
            iCalendar iCal = iCalendar.LoadFromFile(@"Calendars\Recurrence\RRULE18.ics");
            Program.TestCal(iCal);
            Event evt = iCal.Events[0];

            List<Occurrence> occurrences = evt.GetOccurrences(
                new iCalDateTime(1996, 1, 1, tzid, iCal), 
                new iCalDateTime(1998, 3, 1, tzid, iCal));

            iCalDateTime[] DateTimes = new iCalDateTime[]
            {
                new iCalDateTime(1997, 9, 28, 9, 0, 0, tzid, iCal),
                new iCalDateTime(1997, 10, 29, 9, 0, 0, tzid, iCal),
                new iCalDateTime(1997, 11, 28, 9, 0, 0, tzid, iCal),
                new iCalDateTime(1997, 12, 29, 9, 0, 0, tzid, iCal),
                new iCalDateTime(1998, 1, 29, 9, 0, 0, tzid, iCal),
                new iCalDateTime(1998, 2, 26, 9, 0, 0, tzid, iCal)
            };

            string[] TimeZones = new string[]
            {
                "EDT",                
                "EST",
                "EST",
                "EST",
                "EST",
                "EST"                
            };

            for (int i = 0; i < DateTimes.Length; i++)
            {
                iCalDateTime dt = (iCalDateTime)DateTimes[i];
                Assert.AreEqual(dt, occurrences[i].Period.StartTime, "Event should occur on " + dt);
                Assert.AreEqual(TimeZones[i], dt.TimeZoneInfo.TimeZoneName, "Event " + dt + " should occur in the " + TimeZones[i] + " timezone");
            }

            Assert.AreEqual(
                DateTimes.Length,
                occurrences.Count,
                "There should be exactly " + DateTimes.Length + " occurrences; there were " + occurrences.Count);
        }
Пример #40
0
        public void RECURPARSE5()
        {
            iCalendar iCal = new iCalendar();

            Event evt = iCal.Create<Event>();
            evt.Summary = "Test event";
            evt.Start = new iCalDateTime(2006, 1, 1, 9, 0, 0);
            evt.Duration = new TimeSpan(1, 0, 0);
            evt.AddRecurrencePattern(new RecurrencePattern("Every 10 minutes until 1/1/2006 9:50"));

            List<Occurrence> occurrences = evt.GetOccurrences(
                new iCalDateTime(2006, 1, 1), 
                new iCalDateTime(2006, 1, 31));

            iCalDateTime[] DateTimes = new iCalDateTime[]
            {
                new iCalDateTime(2006, 1, 1, 9, 0, 0),
                new iCalDateTime(2006, 1, 1, 9, 10, 0),
                new iCalDateTime(2006, 1, 1, 9, 20, 0),
                new iCalDateTime(2006, 1, 1, 9, 30, 0),
                new iCalDateTime(2006, 1, 1, 9, 40, 0),
                new iCalDateTime(2006, 1, 1, 9, 50, 0)
            };

            for (int i = 0; i < DateTimes.Length; i++)
                Assert.AreEqual(DateTimes[i], occurrences[i].Period.StartTime, "Event should occur on " + DateTimes[i]);

            Assert.AreEqual(
                DateTimes.Length,
                occurrences.Count,
                "There should be exactly " + DateTimes.Length +
                " occurrences; there were " + occurrences.Count);
        }
Пример #41
0
        /// <summary>
        /// Returns true if <paramref name="dt"/> is a date/time that aligns to (occurs within)
        /// the recurrence pattern of this Recur, false otherwise.
        /// </summary>
        public bool IsValidDate(iCalDateTime dt)
        {
            if (BySecond.Count != 0 && !BySecond.Contains(dt.Value.Second)) return false;
            if (ByMinute.Count != 0 && !ByMinute.Contains(dt.Value.Minute)) return false;
            if (ByHour.Count != 0 && !ByHour.Contains(dt.Value.Hour)) return false;
            if (ByDay.Count != 0)
            {
                bool found = false;
                foreach (DaySpecifier bd in ByDay)
                {
                    if (bd.CheckValidDate(this, dt))
                    {
                        found = true;
                        break;
                    }
                }
                if (!found)
                    return false;
            }
            if (ByWeekNo.Count != 0)
            {
                bool found = false;
                int lastWeekOfYear = _Calendar.GetWeekOfYear(new DateTime(dt.Value.Year, 12, 31), System.Globalization.CalendarWeekRule.FirstFourDayWeek, Wkst);
                int currWeekNo = _Calendar.GetWeekOfYear(dt.Value, System.Globalization.CalendarWeekRule.FirstFourDayWeek, Wkst);
                foreach (int WeekNo in ByWeekNo)
                {
                    if ((WeekNo > 0 && WeekNo == currWeekNo) ||
                        (WeekNo < 0 && lastWeekOfYear + WeekNo + 1 == currWeekNo))
                        found = true;
                }
                if (!found)
                    return false;
            }
            if (ByMonth.Count != 0 && !ByMonth.Contains(dt.Value.Month)) return false;
            if (ByMonthDay.Count != 0)
            {
                // Handle negative days of month (count backwards from the end)
                // NOTE: fixes RRULE18 eval
                bool found = false;
                int DaysInMonth = _Calendar.GetDaysInMonth(dt.Value.Year, dt.Value.Month);
                foreach (int Day in ByMonthDay)
                {
                    if ((Day > 0) && (Day == dt.Value.Day))
                        found = true;
                    else if ((Day < 0) && (DaysInMonth + Day + 1 == dt.Value.Day))
                        found = true;
                }

                if (!found)
                    return false;
            }
            if (ByYearDay.Count != 0)
            {
                // Handle negative days of year (count backwards from the end)
                // NOTE: fixes RRULE25 eval
                bool found = false;
                int DaysInYear = _Calendar.GetDaysInYear(dt.Value.Year);
                DateTime baseDate = new DateTime(dt.Value.Year, 1, 1);

                foreach (int Day in ByYearDay)
                {
                    if (Day > 0 && dt.Value.Date == baseDate.AddDays(Day - 1))
                        found = true;
                    else if (Day < 0 && dt.Value.Date == baseDate.AddYears(1).AddDays(Day))
                        found = true;
                }
                if (!found)
                    return false;
            }
            return true;
        }
Пример #42
0
        /// <summary>
        /// Gets a list of alarm occurrences for the given recurring component, <paramref name="rc"/>
        /// that occur between <paramref name="FromDate"/> and <paramref name="ToDate"/>.
        /// </summary>
        virtual public List<AlarmOccurrence> GetOccurrences(RecurringComponent rc, iCalDateTime FromDate, iCalDateTime ToDate)
        {
            Occurrences.Clear();

            // If the trigger is relative, it can recur right along with
            // the recurring items, otherwise, it happens once and
            // only once (at a precise time).
            if (Trigger.IsRelative)
            {
                // Ensure that "FromDate" has already been set
                if (FromDate == null)
                    FromDate = rc.Start.Copy();

                Duration d = null;
                foreach (Occurrence o in rc.GetOccurrences(FromDate, ToDate))
                {
                    iCalDateTime dt = o.Period.StartTime;
                    if (Trigger.Related == Trigger.TriggerRelation.End)
                    {
                        if (o.Period.EndTime != null)
                        {
                            dt = o.Period.EndTime;
                            if (d == null)
                                d = o.Period.Duration;
                        }
                        // Use the "last-found" duration as a reference point
                        else if (d != null)
                            dt = o.Period.StartTime + d;
                        else throw new ArgumentException("Alarm trigger is relative to the END of the occurrence; however, the occurence has no discernible end.");
                    }

                    Occurrences.Add(new AlarmOccurrence(this, dt + Trigger.Duration, rc));
                }
            }
            else Occurrences.Add(new AlarmOccurrence(this, Trigger.DateTime.Copy(), rc));

            // If a REPEAT and DURATION value were specified,
            // then handle those repetitions here.
            AddRepeatedItems();

            return Occurrences;
        }
Пример #43
0
 protected void EnsureByXXXValues(iCalDateTime StartDate)
 {
     // If the frequency is weekly, and
     // no day of week is specified, use
     // the original date's day of week.
     // NOTE: fixes RRULE7 and RRULE8 handling
     if (Frequency == FrequencyType.Weekly &&
         ByDay.Count == 0)
         this.ByDay.Add(new DaySpecifier(StartDate.Value.DayOfWeek));            
     if (Frequency > FrequencyType.Secondly &&
         this.BySecond.Count == 0 &&
         StartDate.HasTime /* NOTE: Fixes a bug where all-day events have BySecond/ByMinute/ByHour added incorrectly */)
         this.BySecond.Add(StartDate.Value.Second);
     if (Frequency > FrequencyType.Minutely &&
         this.ByMinute.Count == 0 &&
         StartDate.HasTime /* NOTE: Fixes a bug where all-day events have BySecond/ByMinute/ByHour added incorrectly */)
         this.ByMinute.Add(StartDate.Value.Minute);
     if (Frequency > FrequencyType.Hourly &&                
         this.ByHour.Count == 0 &&
         StartDate.HasTime /* NOTE: Fixes a bug where all-day events have BySecond/ByMinute/ByHour added incorrectly */)
         this.ByHour.Add(StartDate.Value.Hour);
     // If neither BYDAY, BYMONTHDAY, or BYYEARDAY is specified,
     // default to the current day of month
     // NOTE: fixes RRULE23 handling, added BYYEARDAY exclusion
     // to fix RRULE25 handling
     if (Frequency > FrequencyType.Weekly &&
         this.ByMonthDay.Count == 0 && 
         this.ByYearDay.Count == 0 && 
         this.ByDay.Count == 0) 
         this.ByMonthDay.Add(StartDate.Value.Day);
     // If neither BYMONTH nor BYYEARDAY is specified, default to
     // the current month
     // NOTE: fixes RRULE25 handling
     if (Frequency > FrequencyType.Monthly &&
         this.ByYearDay.Count == 0 && 
         this.ByDay.Count == 0 &&
         this.ByMonth.Count == 0)
         this.ByMonth.Add(StartDate.Value.Month);
 }
Пример #44
0
        /// <summary>
        /// Polls the <see cref="Alarm"/> component for alarms that have been triggered
        /// since the provided <paramref name="Start"/> date/time.  If <paramref name="Start"/>
        /// is null, all triggered alarms will be returned.
        /// </summary>
        /// <param name="Start">The earliest date/time to poll trigerred alarms for.</param>
        /// <returns>A list of <see cref="AlarmOccurrence"/> objects, each containing a triggered alarm.</returns>
        public List<AlarmOccurrence> Poll(iCalDateTime Start, iCalDateTime End)
        {
            List<AlarmOccurrence> Results = new List<AlarmOccurrence>();

            // Evaluate the alarms to determine the recurrences
            RecurringComponent rc = Parent as RecurringComponent;
            if (rc != null)
            {
                Results.AddRange(GetOccurrences(rc, Start, End));
                Results.Sort();
            }
            return Results;
        }
Пример #45
0
 public void IncrementDate(ref iCalDateTime dt)
 {
     IncrementDate(ref dt, this.Interval);
 }
Пример #46
0
 public override List<AlarmOccurrence> PollAlarms(iCalDateTime Start, iCalDateTime End)
 {
     return null;
 }
Пример #47
0
        protected List<iCalDateTime> GetExtraOccurrences(iCalDateTime StartDate, iCalDateTime AbsEndDate)
        {
            List<iCalDateTime> DateTimes = new List<iCalDateTime>();
            iCalDateTime EndDate = new iCalDateTime(StartDate);
            
            // FIXME: is there a reason for this?
            //AbsEndDate = AbsEndDate.AddSeconds(-1);

            IncrementDate(ref EndDate, 1);
            EndDate = EndDate.AddSeconds(-1);
            if (EndDate > AbsEndDate)
                EndDate = AbsEndDate;

            return CalculateChildOccurrences(StartDate, EndDate);
        }
Пример #48
0
 public override void AddSingleException(iCalDateTime dt)
 {
 }
Пример #49
0
        public List<iCalDateTime> Evaluate(iCalDateTime StartDate, iCalDateTime FromDate, iCalDateTime ToDate)
        {
            List<iCalDateTime> DateTimes = new List<iCalDateTime>();
            DateTimes.AddRange(StaticOccurrences);

            // Create a temporary recurrence for populating 
            // missing information using the 'StartDate'.
            RecurrencePattern r = new RecurrencePattern();
            r.CopyFrom(this);

            // Enforce evaluation engine rules
            r.EnforceEvaluationRestrictions();

            // Fill in missing, necessary ByXXX values
            r.EnsureByXXXValues(StartDate);
                        
            // Get the occurrences
            foreach (iCalDateTime occurrence in r.GetOccurrences(StartDate, FromDate.Copy(), ToDate.Copy(), r.Count))
            {
                // NOTE:
                // Used to be DateTimes.AddRange(r.GetOccurrences(FromDate.Copy(), ToDate, r.Count))
                // By doing it this way, fixes bug #19.
                if (!DateTimes.Contains(occurrence))
                    DateTimes.Add(occurrence);
            }

            // Limit the count of returned recurrences
            if (Count != int.MinValue &&
                DateTimes.Count > Count)
                DateTimes.RemoveRange(Count, DateTimes.Count - Count);

            // Process the UNTIL, and make sure the DateTimes
            // occur between FromDate and ToDate
            for (int i = DateTimes.Count - 1; i >= 0; i--)
            {
                iCalDateTime dt = (iCalDateTime)DateTimes[i];
                if (dt > ToDate ||
                    dt < FromDate)
                    DateTimes.RemoveAt(i);
            }

            // Assign missing values
            foreach (iCalDateTime dt in DateTimes)
                dt.MergeWith(StartDate);

            // Ensure that DateTimes have an assigned time if they occur less than daily
            foreach (iCalDateTime dt in DateTimes)
            {
                if (Frequency < FrequencyType.Daily)
                    dt.HasTime = true;
            }
            
            return DateTimes;
        }
Пример #50
0
 protected override void EvaluateExRule(iCalDateTime FromDate, iCalDateTime ToDate)
 {
 }
Пример #51
0
 public iCalDateTimeSerializer(iCalDateTime dt) : base(dt)
 {
     this.m_DateTime = dt;
 }
Пример #52
0
 public override List<Period> Evaluate(iCalDateTime FromDate, iCalDateTime ToDate)
 {
     List<Period> periods = base.Evaluate(FromDate, ToDate);
     // Add the initial specified date/time for the time zone entry
     periods.Insert(0, new Period(Start, null));
     return periods;
 }