Skip to content

Commit 373b0a7

Browse files
Merge pull request #277 from classyk12/feature/en-NG
Added support for Nigeria working culture (en-NG)
2 parents 9bd5531 + b22eb73 commit 373b0a7

File tree

4 files changed

+200
-3
lines changed

4 files changed

+200
-3
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#region License
2+
3+
//
4+
// Copyright (c) 2011-2012, João Matos Silva <kappy@acydburne.com.pt>
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
19+
#endregion
20+
21+
using System;
22+
using System.Collections.Generic;
23+
using DateTimeExtensions.Common;
24+
25+
namespace DateTimeExtensions.WorkingDays.CultureStrategies
26+
{
27+
[Locale("en-NG")]
28+
public class EN_NGHolidayStrategy : HolidayStrategyBase, IHolidayStrategy
29+
{
30+
public EN_NGHolidayStrategy()
31+
{
32+
InnerHolidays.Add(GlobalHolidays.NewYear);
33+
34+
InnerHolidays.Add(ChristianHolidays.GoodFriday);
35+
InnerHolidays.Add(ChristianHolidays.EasterMonday);
36+
InnerHolidays.Add(ChristianHolidays.Christmas);
37+
38+
InnerHolidays.Add(IndependenceDay);
39+
InnerHolidays.Add(DemocracyDay);
40+
41+
InnerHolidays.Add(GlobalHolidays.InternationalWorkersDay);
42+
InnerHolidays.Add(GlobalHolidays.BoxingDay);
43+
}
44+
45+
protected override IDictionary<DateTime, Holiday> BuildObservancesMap(int year)
46+
{
47+
IDictionary<DateTime, Holiday> holidayMap = new Dictionary<DateTime, Holiday>();
48+
foreach (var innerHoliday in InnerHolidays)
49+
{
50+
var date = innerHoliday.GetInstance(year);
51+
if (date.HasValue)
52+
{
53+
if (holidayMap.ContainsKey(date.Value))
54+
{
55+
// Check to see if holiday falling on the Sunday then moves to the monday, and there is another holiday scheduled for the monday
56+
// Update the Holiday Name of the Monday
57+
holidayMap[date.Value] = innerHoliday;
58+
}
59+
60+
else
61+
{
62+
holidayMap.Add(date.Value, innerHoliday);
63+
}
64+
65+
//if the holiday is a sunday, the holiday is observed on next monday
66+
if (date.Value.DayOfWeek == DayOfWeek.Sunday)
67+
{
68+
holidayMap.AddIfInexistent(date.Value.AddDays(1), innerHoliday);
69+
}
70+
}
71+
}
72+
return holidayMap;
73+
}
74+
75+
// 12 June - Democracy Day
76+
private static Holiday democracyDay;
77+
public static Holiday DemocracyDay
78+
{
79+
get
80+
{
81+
if (democracyDay == null)
82+
{
83+
democracyDay = new FixedHoliday("Democracy Day", 6, 12);
84+
}
85+
return democracyDay;
86+
}
87+
}
88+
89+
// 1st October - Independence Day
90+
private static Holiday independenceDay;
91+
public static Holiday IndependenceDay
92+
{
93+
get
94+
{
95+
if (independenceDay == null)
96+
{
97+
independenceDay = new FixedHoliday("IndependenceDay Day", 10, 1);
98+
}
99+
return independenceDay;
100+
}
101+
}
102+
}
103+
}

src/DateTimeExtensions/WorkingDaysExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,11 @@ public static IDictionary<DateTime, Holiday> AllYearHolidays(this DateTime day,
175175
IWorkingDayCultureInfo workingDayCultureInfo)
176176
{
177177
var holidays = new SortedDictionary<DateTime, Holiday>();
178-
foreach (Holiday holiday in workingDayCultureInfo.GetHolidaysOfYear(day.Year))
178+
var holidaysOfTheYear = workingDayCultureInfo.GetHolidaysOfYear(day.Year).ToList();
179+
foreach (Holiday holiday in holidaysOfTheYear)
179180
{
180181
var date = holiday.GetInstance(day.Year);
181-
if (date.HasValue)
182+
if (date.HasValue && !holidays.ContainsKey(date.Value))
182183
{
183184
holidays.Add(date.Value, holiday);
184185
}

tests/DateTimeExtensions.Tests/FrCaHolidaysTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace DateTimeExtensions.Tests
1010
public class FrCaHolidaysTests
1111
{
1212
[Test]
13-
public void can_get_stratery()
13+
public void can_get_strategy()
1414
{
1515
var dateTimeCulture = new WorkingDayCultureInfo("fr-CA");
1616
var strategy = dateTimeCulture.LocateHolidayStrategy(dateTimeCulture.Name, null);
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Linq;
3+
using DateTimeExtensions.WorkingDays;
4+
using DateTimeExtensions.WorkingDays.CultureStrategies;
5+
using NUnit.Framework;
6+
7+
namespace DateTimeExtensions.Tests
8+
{
9+
[TestFixture]
10+
public class EN_NGHolidayStrategyTests
11+
{
12+
private readonly WorkingDayCultureInfo workingDayCultureInfo = new("en-NG");
13+
14+
[Test]
15+
public void StrategyResolution_Returns_EN_NGHolidayStrategy()
16+
{
17+
var strategy = workingDayCultureInfo.LocateHolidayStrategy(workingDayCultureInfo.Name, null);
18+
Assert.That(strategy, Is.TypeOf<EN_NGHolidayStrategy>());
19+
}
20+
21+
[Test]
22+
public void Holidays_Collection_Count_Is_8()
23+
{
24+
var holidays = workingDayCultureInfo.Holidays;
25+
Assert.That(holidays.Count(), Is.EqualTo(8));
26+
}
27+
28+
[Test]
29+
public void Christmas_On_Sunday_2022_And_BoxingDay_Collision_Behaviour()
30+
{
31+
// 2022: Christmas (25 Dec) is Sunday, Boxing Day (26 Dec) is Monday.
32+
// Verify both dates are present as holidays and final mapping for Dec 26 is Boxing Day.
33+
var year = 2022;
34+
var christmas = new DateTime(year, 12, 25);
35+
var boxingDay = new DateTime(year, 12, 26);
36+
37+
var holidaysMap = new DateTime(year, 1, 1).AllYearHolidays(workingDayCultureInfo);
38+
39+
Assert.Multiple(() =>
40+
{
41+
Assert.That(christmas.DayOfWeek, Is.EqualTo(DayOfWeek.Sunday));
42+
Assert.That(holidaysMap.ContainsKey(christmas), Is.True, "Christmas (original date) should be present.");
43+
Assert.That(holidaysMap.ContainsKey(boxingDay), Is.True, "Boxing Day / observed slot should be present.");
44+
45+
// According to current strategy implementation Boxing Day should be the entry for Dec 26.
46+
Assert.That(holidaysMap[christmas].Name, Is.EqualTo("Christmas"));
47+
Assert.That(holidaysMap[boxingDay].Name, Is.EqualTo("Boxing Day"));
48+
49+
// Both dates should be treated as non-working (holidays / weekend)
50+
Assert.That(christmas.IsWorkingDay(workingDayCultureInfo), Is.False);
51+
Assert.That(boxingDay.IsWorkingDay(workingDayCultureInfo), Is.False);
52+
});
53+
}
54+
55+
[Test]
56+
public void Christmas_Sunday_2033_Is_Observed_On_Monday_2033()
57+
{
58+
// Verify a case where Christmas is on Sunday and the following Monday is considered a holiday.
59+
var christmasSunday = new DateTime(2033, 12, 25);
60+
var observed = christmasSunday.AddDays(1);
61+
62+
Assert.Multiple(() =>
63+
{
64+
Assert.That(christmasSunday.DayOfWeek, Is.EqualTo(DayOfWeek.Sunday));
65+
Assert.That(observed.IsHoliday(workingDayCultureInfo), Is.True, "Following Monday should be observed as a holiday.");
66+
});
67+
}
68+
69+
[Test]
70+
public void Assert_DemocracyDayHoliday_On_Sunday_Observed_On_Monday()
71+
{
72+
// 12-06-2022 Democracy day on a Sunday
73+
var mondayAfterDemocracyDay = new DateTime(2022, 06, 13);
74+
Assert.Multiple(() =>
75+
{
76+
Assert.That(mondayAfterDemocracyDay.DayOfWeek, Is.EqualTo(DayOfWeek.Monday));
77+
Assert.That(mondayAfterDemocracyDay.IsWorkingDay(workingDayCultureInfo), Is.False);
78+
});
79+
}
80+
81+
[Test]
82+
public void Assert_IndependenceDayHolidays_On_Sunday_Observed_On_Monday()
83+
{
84+
// 12-06-2022 Independence day on a Sunday
85+
var mondayAfterIndependenceDay = new DateTime(2028, 10, 02);
86+
Assert.Multiple(() =>
87+
{
88+
Assert.That(mondayAfterIndependenceDay.DayOfWeek, Is.EqualTo(DayOfWeek.Monday));
89+
Assert.That(mondayAfterIndependenceDay.IsWorkingDay(workingDayCultureInfo), Is.False);
90+
});
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)