protected void Page_Load(object sender, EventArgs e) { this.Page.Title = "Recurrence Demo"; // On first load, set some defaults and bind the holiday list box to the defined recurrence holidays if(!Page.IsPostBack) { txtStartDate.Text = new DateTime(DateTime.Today.Year, 1, 1).ToString("G"); txtRRULE.Text = "FREQ=DAILY;INTERVAL=5;COUNT=50"; lbResults.DataTextFormatString = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern + " " + CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern; Recurrence r = new Recurrence(txtRRULE.Text); rpRecurrence.SetRecurrence(r); lblDescription.Text = r.ToDescription(); lbHolidays.DataSource = Recurrence.Holidays; lbHolidays.DataBind(); } }
/// <summary> /// Generate instances for the specified recurrence pattern /// </summary> /// <param name="sender">The sender of the event</param> /// <param name="e">The event parameters</param> protected void btnTest_Click(object sender, EventArgs e) { DateTimeCollection dc; DateTime dt; int start; double elapsed; try { lblCount.Text = lblDescription.Text = String.Empty; if(!DateTime.TryParse(txtStartDate.Text, CultureInfo.CurrentCulture, DateTimeStyles.None, out dt)) { lblCount.Text = "Invalid date/time or RRULE format"; return; } // Define the recurrence rule by parsing the text Recurrence r = new Recurrence(txtRRULE.Text); r.StartDateTime = dt; // Synch the pattern control to the RRULE if not called by the pattern's test button if(sender != btnTestPattern) rpRecurrence.SetRecurrence(r); // Use some limitations to prevent overloading the server or timing out the page if possible if(r.Frequency > RecurFrequency.Hourly && (r.MaximumOccurrences == 0 || r.MaximumOccurrences > 5000)) { r.MaximumOccurrences = 5000; txtRRULE.Text = r.ToString(); } if(r.MaximumOccurrences != 0) { if(r.MaximumOccurrences > 5000) { r.MaximumOccurrences = 5000; txtRRULE.Text = r.ToString(); } } else if(r.Frequency == RecurFrequency.Hourly) { if(r.RecurUntil > r.StartDateTime.AddYears(5)) { r.RecurUntil = r.StartDateTime.AddYears(5); txtRRULE.Text = r.ToString(); } } else if(r.RecurUntil > r.StartDateTime.AddYears(50)) { r.RecurUntil = r.StartDateTime.AddYears(50); txtRRULE.Text = r.ToString(); } // Time the calculation start = System.Environment.TickCount; dc = r.InstancesBetween(r.StartDateTime, DateTime.MaxValue); elapsed = (System.Environment.TickCount - start) / 1000.0; // Bind the results to the list box lbResults.DataSource = dc; lbResults.DataBind(); lblCount.Text = String.Format("Found {0:N0} instances in {1:N2} " + "seconds ({2:N2} instances/second)", dc.Count, elapsed, dc.Count / elapsed); // Show a description of the pattern lblDescription.Text = r.ToDescription(); } catch(Exception ex) { lblCount.Text = ex.Message; } }
/// <summary> /// Describe the recurrence rule by using the ToDescription method /// </summary> /// <param name="sender">The sender of the event</param> /// <param name="e">The event arguments</param> private void btnDescribe_Click(object sender, EventArgs e) { try { Recurrence r = new Recurrence(txtRRULE.Text); MessageBox.Show(r.ToDescription(), "Recurrence Description"); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } }