protected override IDictionary <DateTime, Holiday> BuildObservancesMap(int year) { IDictionary <DateTime, Holiday> holidayMap = new Dictionary <DateTime, Holiday>(); foreach (var innerHoliday in InnerHolidays) { var date = innerHoliday.GetInstance(year); if (date.HasValue) { holidayMap.Add(date.Value, innerHoliday); //May Day occours both in 1st May and 2nd May if (innerHoliday.Equals(GlobalHolidays.MayDay)) { var secondMayDay = new FixedHoliday(innerHoliday.Name + " 2nd Day", 5, 2); var secondMayDayIntance = secondMayDay.GetInstance(year); if (secondMayDayIntance != null) { holidayMap.Add(secondMayDayIntance.Value, secondMayDay); } } } } return(holidayMap); }
public void fixedHolidays() { var fixedHoliday = new FixedHoliday("fixed1", new DayInYear(2, 15)); Assert.AreEqual(new DateTime(2012, 2, 15), fixedHoliday.GetInstance(2012)); var fixedHoliday2 = new FixedHoliday("fixed1", 2, 16); Assert.AreEqual(new DateTime(2012, 2, 16), fixedHoliday2.GetInstance(2012)); }
public void GetDateForYear_ReturnsExpectedValue_WhenDoesntRollToMonday() { // arrange var sut = new FixedHoliday(false, new DateTime(1900, 5, 5)); // act var result = sut.GetDateForYear(2020); // assert Assert.True(result.present); Assert.Equal(new DateTime(2020, 5, 5), result.date); }
public void GetDateForYear_ReturnsExpectedValue_WhenRollsToMonda() { // arrange var sut = new FixedHoliday(true, new DateTime(2019, 3, 22)); // act var result = sut.GetDateForYear(2020); // assert Assert.True(result.present); Assert.Equal(new DateTime(2020, 3, 23), result.date); }
/// <summary> /// Update a holiday item in the collection /// </summary> /// <param name="source">The source of the event</param> /// <param name="e">The event arguments</param> protected void dgHolidays_UpdateCommand(object source, DataGridCommandEventArgs e) { HolidayCollection hc = (HolidayCollection)Session["Holidays"]; if (!Page.IsValid) { return; } DropDownList cboMonth = (DropDownList)e.Item.FindControl("cboMonth"); RadioButton rbFloating = (RadioButton)e.Item.FindControl("rbFloating"); // Create holiday object and replace it in the collection if (rbFloating.Checked) { FloatingHoliday fl = new FloatingHoliday { Month = cboMonth.SelectedIndex + 1, Description = ((TextBox)e.Item.FindControl("txtDescription")).Text, Occurrence = (DayOccurrence)((DropDownList)e.Item.FindControl("cboOccurrence")).SelectedIndex + 1, Weekday = (DayOfWeek)((DropDownList)e.Item.FindControl("cboDayOfWeek")).SelectedIndex, Offset = Convert.ToInt32(((TextBox)e.Item.FindControl("txtOffset")).Text) }; hc[e.Item.ItemIndex] = fl; } else { // See if the day of the month is valid for the month. We won't accept Feb 29th either. int day = Convert.ToInt32(((TextBox)e.Item.FindControl("txtDayOfMonth")).Text); if (day > DateTime.DaysInMonth(2007, cboMonth.SelectedIndex + 1)) { ((RangeValidator)e.Item.FindControl("rvDOM")).IsValid = false; return; } FixedHoliday fx = new FixedHoliday { Month = cboMonth.SelectedIndex + 1, Description = ((TextBox)e.Item.FindControl("txtDescription")).Text, AdjustFixedDate = ((CheckBox)e.Item.FindControl("chkAdjustDate")).Checked, Day = Convert.ToInt32(((TextBox)e.Item.FindControl("txtDayOfMonth")).Text) }; hc[e.Item.ItemIndex] = fx; } dgHolidays.EditItemIndex = -1; dgHolidays.DataSource = hc; dgHolidays.DataBind(); }
/// <summary> /// Perform validation and store the changes /// </summary> private void HolidayPropertiesDlg_Closing(object sender, System.ComponentModel.CancelEventArgs e) { // Ignore on cancel if (this.DialogResult == DialogResult.Cancel) { return; } txtDescription.Text = txtDescription.Text.Trim(); epErrors.Clear(); // We must have a description if (txtDescription.Text.Length == 0) { epErrors.SetError(txtDescription, LR.GetString("EditHAEBlankDesc")); e.Cancel = true; } // Leap years aren't accepted so always use a non-leap year to check the date if (rbFixed.Checked && udcDayOfMonth.Value > DateTime.DaysInMonth(2003, (int)cboMonth.SelectedValue)) { epErrors.SetError(udcDayOfMonth, LR.GetString("EditHAEBadDayOfMonth")); e.Cancel = true; } // If all is good, update the holiday object with the new settings if (!e.Cancel) { if (!rbFixed.Checked) { FloatingHoliday fl = new FloatingHoliday(); holiday = fl; fl.Occurrence = (DayOccurrence)cboOccurrence.SelectedValue; fl.Weekday = (System.DayOfWeek)cboDayOfWeek.SelectedValue; fl.Offset = (int)udcOffset.Value; } else { FixedHoliday fx = new FixedHoliday(); holiday = fx; fx.AdjustFixedDate = chkAdjustDate.Checked; fx.Day = (int)udcDayOfMonth.Value; } holiday.Month = (int)cboMonth.SelectedValue; holiday.Description = txtDescription.Text; } }
/// <summary> /// Perform validation and store the changes /// </summary> private void HolidayPropertiesDlg_Closing(object sender, System.ComponentModel.CancelEventArgs e) { // Ignore on cancel if(this.DialogResult == DialogResult.Cancel) return; txtDescription.Text = txtDescription.Text.Trim(); epErrors.Clear(); // We must have a description if(txtDescription.Text.Length == 0) { epErrors.SetError(txtDescription, LR.GetString("EditHAEBlankDesc")); e.Cancel = true; } // Leap years aren't accepted so always use a non-leap year to check the date if(rbFixed.Checked && udcDayOfMonth.Value > DateTime.DaysInMonth(2003, (int)cboMonth.SelectedValue)) { epErrors.SetError(udcDayOfMonth, LR.GetString("EditHAEBadDayOfMonth")); e.Cancel = true; } // If all is good, update the holiday object with the new settings if(!e.Cancel) { if(!rbFixed.Checked) { FloatingHoliday fl = new FloatingHoliday(); holiday = fl; fl.Occurrence = (DayOccurrence)cboOccurrence.SelectedValue; fl.Weekday = (System.DayOfWeek)cboDayOfWeek.SelectedValue; fl.Offset = (int)udcOffset.Value; } else { FixedHoliday fx = new FixedHoliday(); holiday = fx; fx.AdjustFixedDate = chkAdjustDate.Checked; fx.Day = (int)udcDayOfMonth.Value; } holiday.Month = (int)cboMonth.SelectedValue; holiday.Description = txtDescription.Text; } }
protected override IDictionary<DateTime, Holiday> BuildObservancesMap(int year) { IDictionary<DateTime, Holiday> holidayMap = new Dictionary<DateTime, Holiday>(); foreach (var innerHoliday in InnerHolidays) { var date = innerHoliday.GetInstance(year); if (date.HasValue) { holidayMap.Add(date.Value, innerHoliday); //May Day occours both in 1st May and 2nd May if (innerHoliday.Equals(GlobalHolidays.MayDay)) { var secondMayDay = new FixedHoliday(innerHoliday.Name + " 2nd Day", 5, 2); var secondMayDayIntance = secondMayDay.GetInstance(year); if (secondMayDayIntance != null) { holidayMap.Add(secondMayDayIntance.Value, secondMayDay); } } } } return holidayMap; }
/// <summary> /// Bind data to the edit item template. Since the holiday collection uses the abstract class, we'll /// bind data in here rather than in the HTML since we have to determine the type first. /// </summary> /// <param name="source">The source of the event</param> /// <param name="e">The event arguments</param> protected void dgHolidays_ItemDataBound(object sender, DataGridItemEventArgs e) { if (e.Item.ItemType == ListItemType.EditItem) { // The RecurOptsDataSource class contains static methods that return lists of common values we // can uses as the data sources for these drop down lists. DropDownList cboMonth = (DropDownList)e.Item.FindControl("cboMonth"); cboMonth.DataSource = RecurOptsDataSource.MonthsOfYear; cboMonth.DataTextField = "Display"; cboMonth.DataValueField = "Value"; cboMonth.DataBind(); DropDownList cboOccurrence = (DropDownList)e.Item.FindControl("cboOccurrence"); cboOccurrence.DataSource = RecurOptsDataSource.DayOccurrences; cboOccurrence.DataTextField = "Display"; cboOccurrence.DataValueField = "Value"; cboOccurrence.DataBind(); DropDownList cboDayOfWeek = (DropDownList)e.Item.FindControl("cboDayOfWeek"); cboDayOfWeek.DataSource = RecurOptsDataSource.DayOfWeek; cboDayOfWeek.DataTextField = "Display"; cboDayOfWeek.DataValueField = "Value"; cboDayOfWeek.DataBind(); RadioButton rbFloating = (RadioButton)e.Item.FindControl("rbFloating"); RadioButton rbFixed = (RadioButton)e.Item.FindControl("rbFixed"); HolidayCollection hc = (HolidayCollection)Session["Holidays"]; if (hc[e.Item.ItemIndex] is FloatingHoliday) { FloatingHoliday fl = (FloatingHoliday)hc[e.Item.ItemIndex]; cboOccurrence.SelectedIndex = (int)fl.Occurrence - 1; cboDayOfWeek.SelectedIndex = (int)fl.Weekday; ((TextBox)e.Item.FindControl("txtOffset")).Text = ((fl.Offset < -999) ? -999 : (fl.Offset > 999) ? 999 : fl.Offset).ToString(); rbFloating.Checked = true; rbFixed.Checked = false; ((TextBox)e.Item.FindControl("txtDayOfMonth")).Enabled = ((RequiredFieldValidator)e.Item.FindControl("rfvDOM")).Enabled = ((RangeValidator)e.Item.FindControl("rvDOM")).Enabled = ((CheckBox)e.Item.FindControl("chkAdjustDate")).Enabled = false; ((TextBox)e.Item.FindControl("txtDayOfMonth")).CssClass = "Disabled"; } else { FixedHoliday fx = (FixedHoliday)hc[e.Item.ItemIndex]; ((TextBox)e.Item.FindControl("txtOffset")).Text = "0"; ((TextBox)e.Item.FindControl("txtDayOfMonth")).Text = fx.Day.ToString(); ((CheckBox)e.Item.FindControl("chkAdjustDate")).Checked = fx.AdjustFixedDate; rbFloating.Checked = false; rbFixed.Checked = true; cboOccurrence.Enabled = cboDayOfWeek.Enabled = ((TextBox)e.Item.FindControl("txtOffset")).Enabled = ((RequiredFieldValidator)e.Item.FindControl("rfvOffset")).Enabled = ((RangeValidator)e.Item.FindControl("rvOffset")).Enabled = false; cboOccurrence.CssClass = cboDayOfWeek.CssClass = ((TextBox)e.Item.FindControl("txtOffset")).CssClass = "Disabled"; } cboMonth.SelectedIndex = hc[e.Item.ItemIndex].Month - 1; ((TextBox)e.Item.FindControl("txtDescription")).Text = hc[e.Item.ItemIndex].Description; } }