Skip to content

Commit

Permalink
optimize code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Feb 28, 2024
1 parent c61d1f5 commit a42eba5
Show file tree
Hide file tree
Showing 125 changed files with 1,766 additions and 1,720 deletions.
4 changes: 2 additions & 2 deletions src/Nager.Date.UnitTest/Common/LogicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ public void CheckIsOfficialPublicHolidayByCounty2()
[TestMethod]
public void CheckGlobalSwtichWork()
{
var publicHoliday = new PublicHoliday(2020, 01, 30, "Test", "Test", CountryCode.AT, null, null, PublicHolidayType.Public);
var publicHoliday = new Holiday(2020, 01, 30, "Test", "Test", CountryCode.AT, null, null, HolidayTypes.Public);

Assert.IsTrue(publicHoliday.NationalHoliday);

publicHoliday = new PublicHoliday(2020, 01, 30, "Test", "Test", CountryCode.AT, null, ["AT-1"], PublicHolidayType.Public);
publicHoliday = new Holiday(2020, 01, 30, "Test", "Test", CountryCode.AT, null, ["AT-1"], HolidayTypes.Public);

Assert.IsFalse(publicHoliday.NationalHoliday);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nager.Date.UnitTest/MockPublicHolidayProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public DateTime EasterSunday(int year)
return this._catholicProvider.EasterSunday(year);
}

public IEnumerable<PublicHoliday> GetHolidays(int year)
public IEnumerable<Holiday> GetHolidays(int year)
{
throw new NotImplementedException();
}
Expand Down
18 changes: 9 additions & 9 deletions src/Nager.Date/DateSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public static bool ParseCountryCode(string countryCode, out CountryCode parsedCo
/// <param name="countryCode">Country Code (ISO 3166-1 ALPHA-2)</param>
/// <returns>Set of public holidays for given country and year</returns>
/// <exception cref="System.ArgumentException">Thrown when given country code is not recognized valid</exception>
public static IEnumerable<PublicHoliday> GetPublicHolidays(int year, string countryCode)
public static IEnumerable<Holiday> GetPublicHolidays(int year, string countryCode)
{
if (!ParseCountryCode(countryCode, out var parsedCountryCode))
{
Expand All @@ -290,7 +290,7 @@ public static IEnumerable<PublicHoliday> GetPublicHolidays(int year, string coun
/// <param name="year">The year</param>
/// <param name="countryCode">Country Code (ISO 3166-1 ALPHA-2)</param>
/// <returns>Set of public holidays for given country and year</returns>
public static IEnumerable<PublicHoliday> GetPublicHolidays(int year, CountryCode countryCode)
public static IEnumerable<Holiday> GetPublicHolidays(int year, CountryCode countryCode)
{
var provider = GetPublicHolidayProvider(countryCode);
return provider.GetHolidays(year);
Expand All @@ -308,7 +308,7 @@ public static IEnumerable<PublicHoliday> GetPublicHolidays(int year, CountryCode
/// <param name="countryCode">Country Code (ISO 3166-1 ALPHA-2)</param>
/// <returns>Set of public holidays for given country and date range</returns>
/// <exception cref="System.ArgumentException">Thrown when given country code is not recognized valid</exception>
public static IEnumerable<PublicHoliday> GetPublicHolidays(DateTime startDate, DateTime endDate, string countryCode)
public static IEnumerable<Holiday> GetPublicHolidays(DateTime startDate, DateTime endDate, string countryCode)
{
if (!ParseCountryCode(countryCode, out var parsedCountryCode))
{
Expand All @@ -326,7 +326,7 @@ public static IEnumerable<PublicHoliday> GetPublicHolidays(DateTime startDate, D
/// <param name="countryCode">Country Code (ISO 3166-1 ALPHA-2)</param>
/// <returns>Set of public holidays for given country and date range</returns>
/// <exception cref="System.ArgumentException">Thrown when given end date is before given start date</exception>
public static IEnumerable<PublicHoliday> GetPublicHolidays(DateTime startDate, DateTime endDate, CountryCode countryCode)
public static IEnumerable<Holiday> GetPublicHolidays(DateTime startDate, DateTime endDate, CountryCode countryCode)
{
if (startDate > endDate)
{
Expand Down Expand Up @@ -356,9 +356,9 @@ public static IEnumerable<PublicHoliday> GetPublicHolidays(DateTime startDate, D
/// <param name="startDate">The start date</param>
/// <param name="endDate">The end date</param>
/// <returns>Set of public holidays for date range</returns>
public static IEnumerable<PublicHoliday> GetPublicHolidays(DateTime startDate, DateTime endDate)
public static IEnumerable<Holiday> GetPublicHolidays(DateTime startDate, DateTime endDate)
{
var items = new List<PublicHoliday>();
var items = new List<Holiday>();

foreach (var publicHolidayProvider in _publicHolidaysProviders.Keys)
{
Expand All @@ -372,12 +372,12 @@ public static IEnumerable<PublicHoliday> GetPublicHolidays(DateTime startDate, D

#region Check if a date is a Public Holiday

private static Func<PublicHoliday, bool> GetPublicHolidayFilter(DateTime date, string countyCode = null)
private static Func<Holiday, bool> GetPublicHolidayFilter(DateTime date, string countyCode = null)
{
return o => o.Date == date.Date
&& (o.SubdivisionCodes == null || countyCode != null && o.SubdivisionCodes.Contains(countyCode))
&& (o.LaunchYear == null || date.Year >= o.LaunchYear)
&& o.HolidayTypes.HasFlag(PublicHolidayType.Public);
&& o.HolidayTypes.HasFlag(HolidayTypes.Public);
}

/// <summary>
Expand Down Expand Up @@ -419,7 +419,7 @@ public static bool IsPublicHoliday(DateTime date, CountryCode countryCode)
/// True if given date is public holiday in given country, false otherwise.
/// Set of public holidays for given day is returned in out parameter.
/// </returns>
public static bool IsPublicHoliday(DateTime date, CountryCode countryCode, out PublicHoliday[] publicHolidays)
public static bool IsPublicHoliday(DateTime date, CountryCode countryCode, out Holiday[] publicHolidays)
{
var items = GetPublicHolidays(date.Year, countryCode);
publicHolidays = items.Where(GetPublicHolidayFilter(date)).ToArray();
Expand Down
8 changes: 4 additions & 4 deletions src/Nager.Date/Extensions/ListExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Nager.Date.Extensions
internal static class ListExtension
{
internal static void AddIfNotNull(
this List<PublicHoliday> holidays,
PublicHoliday publicHoliday)
this List<Holiday> holidays,
Holiday publicHoliday)
{
if (publicHoliday == null)
{
Expand All @@ -18,8 +18,8 @@ internal static void AddIfNotNull(
}

internal static void AddRangeIfNotNull(
this List<PublicHoliday> holidays,
PublicHoliday[] publicHolidays)
this List<Holiday> holidays,
Holiday[] publicHolidays)
{
if (publicHolidays == null)
{
Expand Down
26 changes: 13 additions & 13 deletions src/Nager.Date/HolidayProviders/AlandProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ public AlandProvider(ICatholicProvider catholicProvider)
}

///<inheritdoc/>
public IEnumerable<PublicHoliday> GetHolidays(int year)
public IEnumerable<Holiday> GetHolidays(int year)
{
var countryCode = CountryCode.AX;

var thirdFridayInJune = DateSystem.FindDay(year, Month.June, DayOfWeek.Friday, Occurrence.Third);
var thirdSaturdayInJune = DateSystem.FindDay(year, Month.June, DayOfWeek.Saturday, Occurrence.Third);
var firstSaturdayInNovember = DateSystem.FindDay(year, Month.November, DayOfWeek.Saturday, Occurrence.First);

var items = new List<PublicHoliday>();
items.Add(new PublicHoliday(year, 1, 1, "Nyårsdagen", "New Year's Day", countryCode));
items.Add(new PublicHoliday(year, 6, 1, "Trettondagen", "Epiphany", countryCode));
var items = new List<Holiday>();
items.Add(new Holiday(year, 1, 1, "Nyårsdagen", "New Year's Day", countryCode));
items.Add(new Holiday(year, 6, 1, "Trettondagen", "Epiphany", countryCode));
items.Add(this._catholicProvider.GoodFriday("Långfredag", year, countryCode));
items.Add(this._catholicProvider.EasterSunday("Påskdagen", year, countryCode).SetLaunchYear(1642));
items.Add(this._catholicProvider.EasterMonday("Annandag påsk", year, countryCode).SetLaunchYear(1642));
items.Add(new PublicHoliday(year, 5, 1, "Första maj", "May Day", countryCode));
items.Add(new Holiday(year, 5, 1, "Första maj", "May Day", countryCode));
items.Add(this._catholicProvider.AscensionDay("Kristi himmelsfärdsdagn", year, countryCode));
items.Add(this._catholicProvider.Pentecost("Pingstdagen", year, countryCode));
items.Add(new PublicHoliday(year, 6, 9, "Självstyrelsedagen", "Autonomy Day", countryCode));
items.Add(new PublicHoliday(thirdFridayInJune, "Midsommarafton", "Midsummer Eve", countryCode));
items.Add(new PublicHoliday(thirdSaturdayInJune, "Midsommardagen", "Midsummer Day", countryCode));
items.Add(new PublicHoliday(firstSaturdayInNovember, "Alla helgons dag", "All Saints Day", countryCode));
items.Add(new PublicHoliday(year, 12, 6, "Självständighetsdagen", "Independence Day", countryCode));
items.Add(new PublicHoliday(year, 12, 24, "Julafton", "Christmas Eve", countryCode));
items.Add(new PublicHoliday(year, 12, 25, "Juldagen", "Christmas Day", countryCode));
items.Add(new PublicHoliday(year, 12, 26, "Annandag jul", "St. Stephen's Day", countryCode));
items.Add(new Holiday(year, 6, 9, "Självstyrelsedagen", "Autonomy Day", countryCode));
items.Add(new Holiday(thirdFridayInJune, "Midsommarafton", "Midsummer Eve", countryCode));
items.Add(new Holiday(thirdSaturdayInJune, "Midsommardagen", "Midsummer Day", countryCode));
items.Add(new Holiday(firstSaturdayInNovember, "Alla helgons dag", "All Saints Day", countryCode));
items.Add(new Holiday(year, 12, 6, "Självständighetsdagen", "Independence Day", countryCode));
items.Add(new Holiday(year, 12, 24, "Julafton", "Christmas Eve", countryCode));
items.Add(new Holiday(year, 12, 25, "Juldagen", "Christmas Day", countryCode));
items.Add(new Holiday(year, 12, 26, "Annandag jul", "St. Stephen's Day", countryCode));

return items.OrderBy(o => o.Date);
}
Expand Down
26 changes: 13 additions & 13 deletions src/Nager.Date/HolidayProviders/AlbaniaProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,35 @@ public AlbaniaProvider(ICatholicProvider catholicProvider, IOrthodoxProvider ort
}

///<inheritdoc/>
public IEnumerable<PublicHoliday> GetHolidays(int year)
public IEnumerable<Holiday> GetHolidays(int year)
{
var countryCode = CountryCode.AL;

var items = new List<PublicHoliday>();
items.Add(this.ApplyShiftingRules(new PublicHoliday(year, 1, 1, "Viti i Ri", "New Year's Day", countryCode)));
items.Add(this.ApplyShiftingRules(new PublicHoliday(year, 1, 2, "Viti i Ri", "New Year's Day", countryCode)));
items.Add(this.ApplyShiftingRules(new PublicHoliday(year, 3, 14, "Dita e Verës", "Summer Day", countryCode)));
items.Add(this.ApplyShiftingRules(new PublicHoliday(year, 3, 22, "Dita e Sulltan Nevruzit", "Nowruz", countryCode)));
var items = new List<Holiday>();
items.Add(this.ApplyShiftingRules(new Holiday(year, 1, 1, "Viti i Ri", "New Year's Day", countryCode)));
items.Add(this.ApplyShiftingRules(new Holiday(year, 1, 2, "Viti i Ri", "New Year's Day", countryCode)));
items.Add(this.ApplyShiftingRules(new Holiday(year, 3, 14, "Dita e Verës", "Summer Day", countryCode)));
items.Add(this.ApplyShiftingRules(new Holiday(year, 3, 22, "Dita e Sulltan Nevruzit", "Nowruz", countryCode)));
//Catholic Easter and monday
items.Add(this._catholicProvider.EasterSunday("Pashkët Katolike", year, countryCode));
items.Add(this._catholicProvider.EasterMonday("Hënen e Pashkët Katolike", year, countryCode));
//Orthodox easter and monday
items.Add(this._orthodoxProvider.EasterSunday("Pashkët Ortodokse", year, countryCode));
items.Add(this._orthodoxProvider.EasterMonday("Hënen e Pashkët Ortodokse", year, countryCode));
items.Add(new PublicHoliday(year, 5, 1, "Dita Ndërkombëtare e Punonjësve", "May Day", countryCode));
items.Add(new Holiday(year, 5, 1, "Dita Ndërkombëtare e Punonjësve", "May Day", countryCode));
//TODO: Eid ul-Fitr is not implemented
//TODO: Eid ul-Adha is not implemented

items.Add(this.ApplyShiftingRules(new PublicHoliday(year, 10, 19, "Dita e Nënë Terezës", "Mother Teresa Day", countryCode)));
items.Add(this.ApplyShiftingRules(new PublicHoliday(year, 11, 28, "Dita e Pavarësisë", "Independence Day", countryCode)));
items.Add(this.ApplyShiftingRules(new PublicHoliday(year, 11, 29, "Dita e Çlirimit", "Liberation Day", countryCode)));
items.Add(this.ApplyShiftingRules(new PublicHoliday(year, 12, 08, "Dita Kombëtare e Rinisë", "Youth Day", countryCode)));
items.Add(this.ApplyShiftingRules(new PublicHoliday(year, 12, 25, "Krishtlindjet", "Christmas Day", countryCode)));
items.Add(this.ApplyShiftingRules(new Holiday(year, 10, 19, "Dita e Nënë Terezës", "Mother Teresa Day", countryCode)));
items.Add(this.ApplyShiftingRules(new Holiday(year, 11, 28, "Dita e Pavarësisë", "Independence Day", countryCode)));
items.Add(this.ApplyShiftingRules(new Holiday(year, 11, 29, "Dita e Çlirimit", "Liberation Day", countryCode)));
items.Add(this.ApplyShiftingRules(new Holiday(year, 12, 08, "Dita Kombëtare e Rinisë", "Youth Day", countryCode)));
items.Add(this.ApplyShiftingRules(new Holiday(year, 12, 25, "Krishtlindjet", "Christmas Day", countryCode)));

return items.OrderBy(o => o.Date);
}

private PublicHoliday ApplyShiftingRules(PublicHoliday holiday)
private Holiday ApplyShiftingRules(Holiday holiday)
{
return holiday
.Shift(saturday => saturday.AddDays(2), sunday => sunday.AddDays(1));
Expand Down
26 changes: 13 additions & 13 deletions src/Nager.Date/HolidayProviders/AndorraProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ public AndorraProvider(ICatholicProvider catholicProvider)
}

///<inheritdoc/>
public IEnumerable<PublicHoliday> GetHolidays(int year)
public IEnumerable<Holiday> GetHolidays(int year)
{
var countryCode = CountryCode.AD;
var easterSunday = this._catholicProvider.EasterSunday(year);

var items = new List<PublicHoliday>();
items.Add(new PublicHoliday(year, 1, 1, "Any nou", "New Year's Day", countryCode));
items.Add(new PublicHoliday(year, 1, 6, "Reis", "Epiphany", countryCode));
items.Add(new PublicHoliday(easterSunday.AddDays(-48), "Carnaval", "Carnival", countryCode));
items.Add(new PublicHoliday(year, 3, 14, "Dia de la Constitució", "Constitution Day", countryCode));
var items = new List<Holiday>();
items.Add(new Holiday(year, 1, 1, "Any nou", "New Year's Day", countryCode));
items.Add(new Holiday(year, 1, 6, "Reis", "Epiphany", countryCode));
items.Add(new Holiday(easterSunday.AddDays(-48), "Carnaval", "Carnival", countryCode));
items.Add(new Holiday(year, 3, 14, "Dia de la Constitució", "Constitution Day", countryCode));
items.Add(this._catholicProvider.GoodFriday("Divendres Sant", year, countryCode));
items.Add(this._catholicProvider.EasterMonday("Dilluns de Pasqua", year, countryCode));
items.Add(new PublicHoliday(year, 5, 1, "Festa del Treball", "Labour Day", countryCode));
items.Add(new Holiday(year, 5, 1, "Festa del Treball", "Labour Day", countryCode));
items.Add(this._catholicProvider.WhitMonday("Dilluns de Pentecosta", year, countryCode));
items.Add(new PublicHoliday(year, 8, 15, "Assumpció", "Assumption Day", countryCode));
items.Add(new PublicHoliday(year, 9, 8, "Mare de Déu de Meritxell", "National Holiday", countryCode));
items.Add(new PublicHoliday(year, 11, 1, "Tots Sants", "All Saints' Day", countryCode));
items.Add(new PublicHoliday(year, 12, 8, "Immaculada Concepció", "Immaculate Conception", countryCode));
items.Add(new PublicHoliday(year, 12, 25, "Nadal", "Christmas Day", countryCode));
items.Add(new PublicHoliday(year, 12, 26, "Sant Esteve", "St. Stephen's Day", countryCode));
items.Add(new Holiday(year, 8, 15, "Assumpció", "Assumption Day", countryCode));
items.Add(new Holiday(year, 9, 8, "Mare de Déu de Meritxell", "National Holiday", countryCode));
items.Add(new Holiday(year, 11, 1, "Tots Sants", "All Saints' Day", countryCode));
items.Add(new Holiday(year, 12, 8, "Immaculada Concepció", "Immaculate Conception", countryCode));
items.Add(new Holiday(year, 12, 25, "Nadal", "Christmas Day", countryCode));
items.Add(new Holiday(year, 12, 26, "Sant Esteve", "St. Stephen's Day", countryCode));

return items.OrderBy(o => o.Date);
}
Expand Down
Loading

0 comments on commit a42eba5

Please sign in to comment.