public Period(iCalDateTime start, TimeSpan duration) : this() { StartTime = start.Copy(); if (duration != TimeSpan.MinValue) { Duration = new Duration(duration); EndTime = start + duration; } }
public Period(iCalDateTime start, iCalDateTime end) : this() { StartTime = start.Copy(); if (end != null) { EndTime = end.Copy(); Duration = new Duration(end.Value - start.Value); } }
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); }
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; }
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; }
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."); } }
public override void CreateInitialize() { base.CreateInitialize(); // Create a new UID for the component UID = UniqueComponent.NewUID(); // Here, we don't simply set to DateTime.Now because DateTime.Now contains milliseconds, and // the iCalendar standard doesn't care at all about milliseconds. Therefore, when comparing // two calendars, one generated, and one loaded from file, they may be functionally identical, // but be determined to be different due to millisecond differences. DateTime now = DateTime.Now; Created = new iCalDateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second); DTStamp = Created.Copy(); }
private void EvaluateToPreviousOccurrence(iCalDateTime completedDate, iCalDateTime currDt) { iCalDateTime beginningDate = completedDate.Copy(); if (RRule != null) foreach (RecurrencePattern rrule in RRule) DetermineStartingRecurrence(rrule, ref beginningDate); if (RDate != null) foreach (RecurrenceDates rdate in RDate) DetermineStartingRecurrence(rdate, ref beginningDate); if (ExRule != null) foreach (RecurrencePattern exrule in ExRule) DetermineStartingRecurrence(exrule, ref beginningDate); if (ExDate != null) foreach (RecurrenceDates exdate in ExDate) DetermineStartingRecurrence(exdate, ref beginningDate); Evaluate(beginningDate, currDt); }