Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#region License

//
// Copyright (c) 2011-2012, João Matos Silva <kappy@acydburne.com.pt>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#endregion

using System;
using System.Collections.Generic;
using DateTimeExtensions.Common;

namespace DateTimeExtensions.WorkingDays.CultureStrategies
{
[Locale("en-NG")]
public class EN_NGHolidayStrategy : HolidayStrategyBase, IHolidayStrategy
{
public EN_NGHolidayStrategy()
{
InnerHolidays.Add(GlobalHolidays.NewYear);

InnerHolidays.Add(ChristianHolidays.GoodFriday);
InnerHolidays.Add(ChristianHolidays.EasterMonday);
InnerHolidays.Add(ChristianHolidays.Christmas);

InnerHolidays.Add(IndependenceDay);
InnerHolidays.Add(DemocracyDay);

InnerHolidays.Add(GlobalHolidays.InternationalWorkersDay);
InnerHolidays.Add(GlobalHolidays.BoxingDay);
}

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)
{
if (holidayMap.ContainsKey(date.Value))
{
// Check to see if holiday falling on the Sunday then moves to the monday, and there is another holiday scheduled for the monday
// Update the Holiday Name of the Monday
holidayMap[date.Value] = innerHoliday;
}

else
{
holidayMap.Add(date.Value, innerHoliday);
}

//if the holiday is a sunday, the holiday is observed on next monday
if (date.Value.DayOfWeek == DayOfWeek.Sunday)
{
holidayMap.AddIfInexistent(date.Value.AddDays(1), innerHoliday);
}
}
}
return holidayMap;
}

// 12 June - Democracy Day
private static Holiday democracyDay;
public static Holiday DemocracyDay
{
get
{
if (democracyDay == null)
{
democracyDay = new FixedHoliday("Democracy Day", 6, 12);
}
return democracyDay;
}
}

// 1st October - Independence Day
private static Holiday independenceDay;
public static Holiday IndependenceDay
{
get
{
if (independenceDay == null)
{
independenceDay = new FixedHoliday("IndependenceDay Day", 10, 1);
}
return independenceDay;
}
}
}
}
5 changes: 3 additions & 2 deletions src/DateTimeExtensions/WorkingDaysExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,11 @@ public static IDictionary<DateTime, Holiday> AllYearHolidays(this DateTime day,
IWorkingDayCultureInfo workingDayCultureInfo)
{
var holidays = new SortedDictionary<DateTime, Holiday>();
foreach (Holiday holiday in workingDayCultureInfo.GetHolidaysOfYear(day.Year))
var holidaysOfTheYear = workingDayCultureInfo.GetHolidaysOfYear(day.Year).ToList();
foreach (Holiday holiday in holidaysOfTheYear)
{
var date = holiday.GetInstance(day.Year);
if (date.HasValue)
if (date.HasValue && !holidays.ContainsKey(date.Value))
{
holidays.Add(date.Value, holiday);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/DateTimeExtensions.Tests/FrCaHolidaysTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace DateTimeExtensions.Tests
public class FrCaHolidaysTests
{
[Test]
public void can_get_stratery()
public void can_get_strategy()
{
var dateTimeCulture = new WorkingDayCultureInfo("fr-CA");
var strategy = dateTimeCulture.LocateHolidayStrategy(dateTimeCulture.Name, null);
Expand Down
93 changes: 93 additions & 0 deletions tests/DateTimeExtensions.Tests/enNGHolidaysTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Linq;
using DateTimeExtensions.WorkingDays;
using DateTimeExtensions.WorkingDays.CultureStrategies;
using NUnit.Framework;

namespace DateTimeExtensions.Tests
{
[TestFixture]
public class EN_NGHolidayStrategyTests
{
private readonly WorkingDayCultureInfo workingDayCultureInfo = new("en-NG");

[Test]
public void StrategyResolution_Returns_EN_NGHolidayStrategy()
{
var strategy = workingDayCultureInfo.LocateHolidayStrategy(workingDayCultureInfo.Name, null);
Assert.That(strategy, Is.TypeOf<EN_NGHolidayStrategy>());
}

[Test]
public void Holidays_Collection_Count_Is_8()
{
var holidays = workingDayCultureInfo.Holidays;
Assert.That(holidays.Count(), Is.EqualTo(8));
}

[Test]
public void Christmas_On_Sunday_2022_And_BoxingDay_Collision_Behaviour()
{
// 2022: Christmas (25 Dec) is Sunday, Boxing Day (26 Dec) is Monday.
// Verify both dates are present as holidays and final mapping for Dec 26 is Boxing Day.
var year = 2022;
var christmas = new DateTime(year, 12, 25);
var boxingDay = new DateTime(year, 12, 26);

var holidaysMap = new DateTime(year, 1, 1).AllYearHolidays(workingDayCultureInfo);

Assert.Multiple(() =>
{
Assert.That(christmas.DayOfWeek, Is.EqualTo(DayOfWeek.Sunday));
Assert.That(holidaysMap.ContainsKey(christmas), Is.True, "Christmas (original date) should be present.");
Assert.That(holidaysMap.ContainsKey(boxingDay), Is.True, "Boxing Day / observed slot should be present.");

// According to current strategy implementation Boxing Day should be the entry for Dec 26.
Assert.That(holidaysMap[christmas].Name, Is.EqualTo("Christmas"));
Assert.That(holidaysMap[boxingDay].Name, Is.EqualTo("Boxing Day"));

// Both dates should be treated as non-working (holidays / weekend)
Assert.That(christmas.IsWorkingDay(workingDayCultureInfo), Is.False);
Assert.That(boxingDay.IsWorkingDay(workingDayCultureInfo), Is.False);
});
}

[Test]
public void Christmas_Sunday_2033_Is_Observed_On_Monday_2033()
{
// Verify a case where Christmas is on Sunday and the following Monday is considered a holiday.
var christmasSunday = new DateTime(2033, 12, 25);
var observed = christmasSunday.AddDays(1);

Assert.Multiple(() =>
{
Assert.That(christmasSunday.DayOfWeek, Is.EqualTo(DayOfWeek.Sunday));
Assert.That(observed.IsHoliday(workingDayCultureInfo), Is.True, "Following Monday should be observed as a holiday.");
});
}

[Test]
public void Assert_DemocracyDayHoliday_On_Sunday_Observed_On_Monday()
{
// 12-06-2022 Democracy day on a Sunday
var mondayAfterDemocracyDay = new DateTime(2022, 06, 13);
Assert.Multiple(() =>
{
Assert.That(mondayAfterDemocracyDay.DayOfWeek, Is.EqualTo(DayOfWeek.Monday));
Assert.That(mondayAfterDemocracyDay.IsWorkingDay(workingDayCultureInfo), Is.False);
});
}

[Test]
public void Assert_IndependenceDayHolidays_On_Sunday_Observed_On_Monday()
{
// 12-06-2022 Independence day on a Sunday
var mondayAfterIndependenceDay = new DateTime(2028, 10, 02);
Assert.Multiple(() =>
{
Assert.That(mondayAfterIndependenceDay.DayOfWeek, Is.EqualTo(DayOfWeek.Monday));
Assert.That(mondayAfterIndependenceDay.IsWorkingDay(workingDayCultureInfo), Is.False);
});
}
}
}
Loading