Skip to content

Commit

Permalink
Fixed NZ Easter-Anzac clash (and added tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
b-h-mck committed Feb 10, 2025
1 parent 7f78c44 commit f83b1c0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,18 @@ protected override IDictionary<DateTime, Holiday> BuildObservancesMap(int year)
var date = innerHoliday.GetInstance(year);
if (date.HasValue)
{
holidayMap.Add(date.Value, innerHoliday);
// If the holiday already exists in the map, combine the two holidays.
// This is apparent for Anzac Day/Easter Monday on 25 April 2011.
if (holidayMap.TryGetValue(date.Value, out var existingHoliday))
{
var combinedHoliday = new FixedHoliday($"{existingHoliday.Name}/{innerHoliday.Name}",
date.Value.Month, date.Value.Day);
holidayMap[date.Value] = combinedHoliday;
}
else
{
holidayMap.Add(date.Value, innerHoliday);
}

// New Year, Day After New Year, Christmas and Boxing Days are 'Mondayised'
// ie if these dates fall on a weekday then they are observed on the actual day.
Expand Down
32 changes: 31 additions & 1 deletion tests/DateTimeExtensions.Tests/en-NZCalendarTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public void NewYearsDay()
{
var workingDayCultureInfo = new WorkingDayCultureInfo("en-NZ");

var dateOnGregorian = new DateTime(2013, 1, 1);
var dateOnGregorian = new DateTime(2011, 1, 1);
TestHoliday(workingDayCultureInfo, dateOnGregorian);
dateOnGregorian = new DateTime(2012, 1, 1);
TestHoliday(workingDayCultureInfo, dateOnGregorian);
dateOnGregorian = new DateTime(2013, 1, 1);
TestHoliday(workingDayCultureInfo, dateOnGregorian);
dateOnGregorian = new DateTime(2014, 1, 1);
TestHoliday(workingDayCultureInfo, dateOnGregorian);
Expand Down Expand Up @@ -104,6 +108,32 @@ public void BoxingDay()
TestHoliday(workingDayCultureInfo, dateOnGregorian);
}

[Test]
public void EasterAnzacClash()
{
var workingDayCultureInfo = new WorkingDayCultureInfo("en-NZ");

var holidays = workingDayCultureInfo.GetHolidaysOfYear(2010);
Assert.IsTrue(holidays.Any(h => h.Name == "Easter Monday"));
Assert.IsTrue(holidays.Any(h => h.Name == "Anzac Day"));
Assert.IsFalse(holidays.Any(h => h.Name == "Easter Monday/Anzac Day"));

holidays = workingDayCultureInfo.GetHolidaysOfYear(2011);
Assert.IsFalse(holidays.Any(h => h.Name == "Easter Monday"));
Assert.IsFalse(holidays.Any(h => h.Name == "Anzac Day"));
Assert.IsTrue(holidays.Any(h => h.Name == "Easter Monday/Anzac Day"));

holidays = workingDayCultureInfo.GetHolidaysOfYear(2012);
Assert.IsTrue(holidays.Any(h => h.Name == "Easter Monday"));
Assert.IsTrue(holidays.Any(h => h.Name == "Anzac Day"));
Assert.IsFalse(holidays.Any(h => h.Name == "Easter Monday/Anzac Day"));

holidays = workingDayCultureInfo.GetHolidaysOfYear(2095);
Assert.IsFalse(holidays.Any(h => h.Name == "Easter Monday"));
Assert.IsFalse(holidays.Any(h => h.Name == "Anzac Day"));
Assert.IsTrue(holidays.Any(h => h.Name == "Easter Monday/Anzac Day"));
}


private void TestHoliday(IWorkingDayCultureInfo workingDayCultureInfo, DateTime dateOnGregorian)
{
Expand Down

0 comments on commit f83b1c0

Please sign in to comment.