public static long DateDiff(Recur.FrequencyType frequency, Date_Time dt1, Date_Time dt2, DayOfWeek firstDayOfWeek) { if (frequency == Recur.FrequencyType.YEARLY) { return(dt2.Year - dt1.Year); } if (frequency == Recur.FrequencyType.MONTHLY) { return((dt2.Month - dt1.Month) + (12 * (dt2.Year - dt1.Year))); } if (frequency == Recur.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 Date_Time 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 == Recur.FrequencyType.DAILY) { return(Round(ts.TotalDays)); } if (frequency == Recur.FrequencyType.HOURLY) { return(Round(ts.TotalHours)); } if (frequency == Recur.FrequencyType.MINUTELY) { return(Round(ts.TotalMinutes)); } if (frequency == Recur.FrequencyType.SECONDLY) { return(Round(ts.TotalSeconds)); } return(0); }
public static Date_Time AddFrequency(Recur.FrequencyType frequency, Date_Time dt, int interval) { switch (frequency) { case Recur.FrequencyType.YEARLY: return dt.AddYears(interval); case Recur.FrequencyType.MONTHLY: return dt.AddMonths(interval); case Recur.FrequencyType.WEEKLY: return dt.AddDays(interval * 7); case Recur.FrequencyType.DAILY: return dt.AddDays(interval); case Recur.FrequencyType.HOURLY: return dt.AddHours(interval); case Recur.FrequencyType.MINUTELY: return dt.AddMinutes(interval); case Recur.FrequencyType.SECONDLY: return dt.AddSeconds(interval); default: return dt; } }
public static Date_Time AddFrequency(Recur.FrequencyType frequency, Date_Time dt, int interval) { switch (frequency) { case Recur.FrequencyType.YEARLY: return(dt.AddYears(interval)); case Recur.FrequencyType.MONTHLY: return(dt.AddMonths(interval)); case Recur.FrequencyType.WEEKLY: return(dt.AddDays(interval * 7)); case Recur.FrequencyType.DAILY: return(dt.AddDays(interval)); case Recur.FrequencyType.HOURLY: return(dt.AddHours(interval)); case Recur.FrequencyType.MINUTELY: return(dt.AddMinutes(interval)); case Recur.FrequencyType.SECONDLY: return(dt.AddSeconds(interval)); default: return(dt); } }
public List <Date_Time> Evaluate(Date_Time StartDate, Date_Time FromDate, Date_Time ToDate) { List <Date_Time> DateTimes = new List <Date_Time>(); DateTimes.AddRange(StaticOccurrences); // 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 (Date_Time dt in StaticOccurrences) { if (FromDate < dt) { FromDate = dt.AddSeconds(1); } } } // 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 frequency is WEEKLY, and the interval is greater than 1, // then we need to ensure that the StartDate occurs in one of the // "active" weeks, to ensure that we properly "step" through weeks. // NOTE: Fixes bug #1741093 - WEEKLY frequency eval behaves strangely if (Frequency == FrequencyType.WEEKLY && Interval > 1) { // Get the week of year of the time frame we want to calculate int firstEvalWeek = m_Calendar.GetWeekOfYear(FromDate.Value, System.Globalization.CalendarWeekRule.FirstFourDayWeek, Wkst); // Count backwards in years, calculating how many weeks' difference we have between // start date and evaluation period. Date_Time evalDate = FromDate; while (evalDate.Year > StartDate.Year) { firstEvalWeek += m_Calendar.GetWeekOfYear(new DateTime(evalDate.Year - 1, 12, 31), System.Globalization.CalendarWeekRule.FirstFourDayWeek, Wkst); evalDate = evalDate.AddYears(-1); } // Determine the difference, in weeks, between the start date and the evaluation period. int startWeek = m_Calendar.GetWeekOfYear(StartDate.Value, System.Globalization.CalendarWeekRule.FirstFourDayWeek, Wkst); int weeksDifference = firstEvalWeek - startWeek; // Determine how many weeks the evaluation period needs to change // in order to "align" to the start date week, given the specified interval. int offset = 0; while (weeksDifference % Interval != 0) { weeksDifference--; offset++; } // Offset the week back to a "compatible" week for evaluation FromDate = FromDate.AddDays(-offset * 7); } // Create a temporary recurrence for populating // missing information using the 'StartDate'. Recur r = new Recur(); r.CopyFrom(this); // If an INTERVAL was not specified, default to 1 r.EnsureInterval(); // Fill in missing, necessary ByXXX values r.EnsureByXXXValues(StartDate); // Get the occurrences foreach (Date_Time occurrence in r.GetOccurrences(FromDate.Copy(), ToDate, r.Count)) { // NOTE: // Used to be DateTime.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--) { Date_Time dt = (Date_Time)DateTimes[i]; if (dt > ToDate || dt < FromDate) { DateTimes.RemoveAt(i); } } // Assign missing values foreach (Date_Time dt in DateTimes) { dt.MergeWith(StartDate); } // Ensure that DateTimes have an assigned time if they occur less than dailyB foreach (Date_Time dt in DateTimes) { if (Frequency < FrequencyType.DAILY) { dt.HasTime = true; } } return(DateTimes); }