Skip to content

Commit

Permalink
optimize logic for CH-AR, CH-AI, CH-NE
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Oct 1, 2024
1 parent e35ebad commit d1cf49b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/Nager.Date.UnitTest/Common/CountryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void HolidayProvider_ReturnCorrectCountryCode()
}

[TestMethod]
public void CheckCounties()
public void CheckSubdivisonCodes()
{
var failures = new List<string>();

Expand All @@ -54,6 +54,12 @@ public void CheckCounties()
continue;
}

var groupedSubdivisonCodes = publicHoliday.SubdivisionCodes.GroupBy(subdivisionCode => subdivisionCode).Select(subdivisionCodes => new { subdivisionCodes.Key, Count = subdivisionCodes.Count() });
if (groupedSubdivisonCodes.Where(o => o.Count > 1).Any())
{
failures.Add($"{countryCode} - Duplicate SubdivisonCode by {publicHoliday}");
}

if (publicHoliday.SubdivisionCodes.Count(o => subdivisionCodes.Keys.Contains(o)) != publicHoliday.SubdivisionCodes.Length)
{
var diff = publicHoliday.SubdivisionCodes.Except(subdivisionCodes.Keys);
Expand Down
25 changes: 16 additions & 9 deletions src/Nager.Date/HolidayProviders/SwitzerlandHolidayProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ private HolidaySpecification BerchtoldsDay(int year)
{
var subdivisionCodes = new List<string>(["CH-BE", "CH-FR", "CH-SH", "CH-AG", "CH-TG", "CH-VD"]);

if (new DateTime(year, 12, 25).DayOfWeek == DayOfWeek.Sunday ||
new DateTime(year, 1, 1).DayOfWeek == DayOfWeek.Sunday)
if (new DateTime(year, 1, 1).DayOfWeek == DayOfWeek.Sunday ||
new DateTime(year, 12, 25).DayOfWeek == DayOfWeek.Sunday)
{
subdivisionCodes.Add("CH-NE");
}
Expand Down Expand Up @@ -339,12 +339,19 @@ private HolidaySpecification[] StephensDay(int year)
var englishName = "St. Stephen's Day";
var localName = "Stephanstag";

//TODO: CH-AR logik
//Appenzell AR: “The second day of Christmas is not celebrated if the first day of Christmas falls on a Monday or Friday.” -> 26.12. is not a public holiday if it is a Tuesday or Saturday -- as in Appenzell IR
//TODO: CH-AI logik
//Appenzell IR: Appenzell IR: “St. Stephen's Day, unless its celebration results in three consecutive days of rest” -> 26.12. is not a public holiday if it is a Tuesday or Saturday -- as in Appenzell AR
//TODO: CH-NE logik
//Neuchâtel: public holiday if 1.1. or 25.12. is a Sunday
var subdivisionCodes = new List<string>(["CH-ZH", "CH-BE", "CH-LU", "CH-GL", "CH-FR", "CH-BS", "CH-BL", "CH-SH", "CH-SG", "CH-GR", "CH-AG", "CH-TG", "CH-TI"]);

if (new DateTime(year, 12, 26).DayOfWeek != DayOfWeek.Tuesday &&
new DateTime(year, 12, 26).DayOfWeek != DayOfWeek.Saturday)
{
subdivisionCodes.AddRange(["CH-AR", "CH-AI"]);
}

if (new DateTime(year, 1, 1).DayOfWeek == DayOfWeek.Sunday ||
new DateTime(year, 12, 25).DayOfWeek == DayOfWeek.Sunday)
{
subdivisionCodes.Add("CH-NE");
}

return
[
Expand All @@ -354,7 +361,7 @@ private HolidaySpecification[] StephensDay(int year)
EnglishName = englishName,
LocalName = localName,
HolidayTypes = HolidayTypes.Public,
SubdivisionCodes = ["CH-ZH", "CH-BE", "CH-LU", "CH-GL", "CH-FR", "CH-BS", "CH-BL", "CH-SH", "CH-AR", "CH-AI", "CH-SG", "CH-GR", "CH-AG", "CH-TG", "CH-TI", "CH-NE" ]
SubdivisionCodes = [.. subdivisionCodes]
},
new HolidaySpecification
{
Expand Down

0 comments on commit d1cf49b

Please sign in to comment.