-
Notifications
You must be signed in to change notification settings - Fork 3
Implements a Multi Calculated Holiday based on you BL
Paolo Innocenti edited this page Aug 3, 2021
·
1 revision
A multi-calculated holiday is an abstract class that allows you in a single step to generate a list of Holiday Dates based on the year passed to the Utility.
/// <summary>
/// An example implementation of <see cref="MultiCalculatedHoliDay"/>
/// </summary>
/// <seealso cref="PH.WorkingDaysAndTimeUtility.Configuration.MultiCalculatedHoliDay" />
public class WorkingOnSaturdayIfOdd : MultiCalculatedHoliDay
{
internal WorkingOnSaturdayIfOdd() : base(0, 0)
{
}
public override Type GetHolyDayType() => typeof(WorkingOnSaturdayIfOdd);
/// <summary>Calculates the list of MultiCalculatedHoliDays for the given year.</summary>
/// <param name="year">The year.</param>
/// <returns></returns>
public override List<DateTime> CalculateList(int year)
{
var first = new DateTime(year, 1, 1, 0, 0, 0);
var last = new DateTime(year +1, 1, 1, 0, 0, 0);
CultureInfo myCI = new CultureInfo("it-IT");
var baseHolidays = new List<DateTime>();
int add = 1;
while (first < last)
{
if (first.DayOfWeek == DayOfWeek.Saturday)
{
add = 7;
var weekNumber = myCI.Calendar.GetWeekOfYear(first, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
if (weekNumber % 2 == 0)
{
baseHolidays.Add(first);
}
}
first = first.AddDays(add);
}
return baseHolidays;
}
}