/// <summary> /// Gets the Friendly Text of the Calendar Event. /// For example, "Every 3 days at 10:30am", "Monday, Wednesday, Friday at 5:00pm", "Saturday at 4:30pm" /// </summary> /// <param name="condensed">if set to <c>true</c> [condensed].</param> /// <returns> /// A <see cref="System.String" /> containing a friendly description of the Schedule. /// </returns> public string ToFriendlyScheduleText(bool condensed) { // init the result to just the schedule name just in case we can't figure out the FriendlyText string result = this.Name; DDay.iCal.Event calendarEvent = this.GetCalenderEvent(); if (calendarEvent != null && calendarEvent.DTStart != null) { string startTimeText = calendarEvent.DTStart.Value.TimeOfDay.ToTimeString(); if (calendarEvent.RecurrenceRules.Any()) { // some type of recurring schedule IRecurrencePattern rrule = calendarEvent.RecurrenceRules[0]; switch (rrule.Frequency) { case FrequencyType.Daily: result = "Daily"; if (rrule.Interval > 1) { result += string.Format(" every {0} days", rrule.Interval); } result += " at " + startTimeText; break; case FrequencyType.Weekly: result = rrule.ByDay.Select(a => a.DayOfWeek.ConvertToString()).ToList().AsDelimited(","); if (string.IsNullOrEmpty(result)) { // no day selected, so it has an incomplete schedule return("No Scheduled Days"); } if (rrule.Interval > 1) { result += string.Format(" every {0} weeks", rrule.Interval); } result += " at " + startTimeText; break; case FrequencyType.Monthly: if (rrule.ByMonthDay.Count > 0) { // Day X of every X Months (we only support one day in the ByMonthDay list) int monthDay = rrule.ByMonthDay[0]; result = string.Format("Day {0} of every ", monthDay); if (rrule.Interval > 1) { result += string.Format("{0} months", rrule.Interval); } else { result += "month"; } result += " at " + startTimeText; } else if (rrule.ByDay.Count > 0) { // The Nth <DayOfWeekName> (we only support one day in the ByDay list) IWeekDay bydate = rrule.ByDay[0]; if (NthNames.ContainsKey(bydate.Offset)) { result = string.Format("The {0} {1} of every month", NthNames[bydate.Offset], bydate.DayOfWeek.ConvertToString()); } else { // unsupported case (just show the name) } result += " at " + startTimeText; } else { // unsupported case (just show the name) } break; default: // some other type of recurring type (probably specific dates). Just return the Name of the schedule break; } } else { // not any type of recurring, might be one-time or from specific dates, etc var dates = calendarEvent.GetOccurrences(DateTime.MinValue, DateTime.MaxValue).Where(a => a.Period != null && a.Period.StartTime != null) .Select(a => a.Period.StartTime.Value) .OrderBy(a => a).ToList(); if (dates.Count() > 1) { if (condensed || dates.Count() > 99) { result = string.Format("Multiple dates between {0} and {1}", dates.First().ToShortDateString(), dates.Last().ToShortDateString()); } else { var listHtml = "<ul class='list-unstyled'>" + Environment.NewLine; foreach (var date in dates) { listHtml += string.Format("<li>{0}</li>", date.ToShortDateTimeString()) + Environment.NewLine; } listHtml += "</ul>"; result = listHtml; } } else if (dates.Count() == 1) { result = "Once at " + calendarEvent.DTStart.Value.ToShortDateTimeString(); } else { return("No Schedule"); } } } else { if (WeeklyDayOfWeek.HasValue) { result = WeeklyDayOfWeek.Value.ConvertToString(); if (WeeklyTimeOfDay.HasValue) { result += " at " + WeeklyTimeOfDay.Value.ToTimeString(); } } else { // no start time. Nothing scheduled return("No Schedule"); } } return(result); }
/// <summary> /// Gets the Friendly Text of the Calendar Event. /// For example, "Every 3 days at 10:30am", "Monday, Wednesday, Friday at 5:00pm", "Saturday at 4:30pm" /// </summary> /// <returns>A <see cref="System.String"/> containing a friendly description of the Schedule.</returns> public string ToFriendlyScheduleText() { // init the result to just the schedule name just in case we can't figure out the FriendlyText string result = this.Name; DDay.iCal.Event calendarEvent = this.GetCalenderEvent(); if (calendarEvent != null && calendarEvent.DTStart != null) { string startTimeText = calendarEvent.DTStart.Value.TimeOfDay.ToTimeString(); if (calendarEvent.RecurrenceRules.Any()) { // some type of recurring schedule IRecurrencePattern rrule = calendarEvent.RecurrenceRules[0]; switch (rrule.Frequency) { case FrequencyType.Daily: result = "Daily"; if (rrule.Interval > 1) { result += string.Format(" every {0} days", rrule.Interval); } result += " at " + startTimeText; break; case FrequencyType.Weekly: result = rrule.ByDay.Select(a => a.DayOfWeek.ConvertToString()).ToList().AsDelimited(","); if (string.IsNullOrEmpty(result)) { // no day selected, so it has an incomplete schedule return("No Scheduled Days"); } if (rrule.Interval > 1) { result += string.Format(" every {0} weeks", rrule.Interval); } result += " at " + startTimeText; break; case FrequencyType.Monthly: if (rrule.ByMonthDay.Count > 0) { // Day X of every X Months (we only support one day in the ByMonthDay list) int monthDay = rrule.ByMonthDay[0]; result = string.Format("Day {0} of every ", monthDay); if (rrule.Interval > 1) { result += string.Format("{0} months", rrule.Interval); } else { result += "month"; } result += " at " + startTimeText; } else if (rrule.ByDay.Count > 0) { // The Nth <DayOfWeekName> (we only support one day in the ByDay list) IWeekDay bydate = rrule.ByDay[0]; if (NthNames.ContainsKey(bydate.Offset)) { result = string.Format("The {0} {1} of every month", NthNames[bydate.Offset], bydate.DayOfWeek.ConvertToString()); } else { // unsupported case (just show the name) } result += " at " + startTimeText; } else { // unsupported case (just show the name) } break; default: // some other type of recurring type (probably specific dates). Just return the Name of the schedule break; } } else { // not any type of recurring, runs once result = "Once at " + calendarEvent.DTStart.Value.ToString(); } } else { if (WeeklyDayOfWeek.HasValue) { result = WeeklyDayOfWeek.Value.ConvertToString(); if (WeeklyTimeOfDay.HasValue) { result += " at " + WeeklyTimeOfDay.Value.ToTimeString(); } } else { // no start time. Nothing scheduled return("No Schedule"); } } return(result); }