Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR_CA: Fix dictionary error when weekend holidays in a row #206

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
Expand Up @@ -20,8 +20,6 @@

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

namespace DateTimeExtensions.WorkingDays.CultureStrategies
Expand All @@ -35,44 +33,46 @@ public FR_CAHolidayStrategy()
this.InnerHolidays.Add(GlobalHolidays.NewYear);
this.InnerHolidays.Add(ChristianHolidays.GoodFriday);
this.InnerHolidays.Add(ChristianHolidays.EasterMonday);
//Victoria Day is not really national
//this.InnerHolidays.Add(VictoriaDay);
this.InnerHolidays.Add(CanadaDay);
this.InnerHolidays.Add(LabourDay);
this.InnerHolidays.Add(Thanksgiving);
this.InnerHolidays.Add(RemembranceDay);
this.InnerHolidays.Add(ChristianHolidays.Christmas);
//Boxing is not really national
//this.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)
{
holidayMap.Add(date.Value, innerHoliday);
//if the holiday is a saturday, the holiday is observed on previous friday
holidayMap.AddIfInexistent(date.Value, innerHoliday);

if (date.Value.DayOfWeek == DayOfWeek.Saturday)
{
holidayMap.Add(date.Value.AddDays(2), innerHoliday);
//if the holiday is a saturday, the holiday is observed on previous friday
holidayMap.AddIfInexistent(date.Value.AddDays(-1), innerHoliday);
}
//if the holiday is a sunday, the holiday is observed on next monday

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

//First Monday in September - Canada Day
private static Holiday canadaDay;

/// <summary>
/// July 1 - Canada Day
/// </summary>
public static Holiday CanadaDay
{
get
Expand All @@ -85,9 +85,11 @@ public static Holiday CanadaDay
}
}

//First Monday in September - Labour Day
private static Holiday labourDay;

/// <summary>
/// First Monday in September - Labour Day
/// </summary>
public static Holiday LabourDay
{
get
Expand All @@ -101,25 +103,11 @@ public static Holiday LabourDay
}
}

//Monday on or before May 24 - Victoria Day
private static Holiday victoriaDay;

public static Holiday VictoriaDay
{
get
{
if (victoriaDay == null)
{
// 25 = day after 24. NthDayOfWeekAfterDayHoliday doesn't count the start day
victoriaDay = new NthDayOfWeekAfterDayHoliday("Victoria Day", -1, DayOfWeek.Monday, 5, 25);
}
return victoriaDay;
}
}

//Second Monday in October - Thanksgiving
private static Holiday thanksgiving;

/// <summary>
/// Second Monday in October - Thanksgiving
/// </summary>
public static Holiday Thanksgiving
{
get
Expand All @@ -133,9 +121,11 @@ public static Holiday Thanksgiving
}
}

//November 11 - Remembrance Day
private static Holiday remembranceDay;

/// <summary>
/// November 11 - Remembrance Day
/// </summary>
public static Holiday RemembranceDay
{
get
Expand Down
14 changes: 11 additions & 3 deletions tests/DateTimeExtensions.Tests/FrCaHolidaysTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DateTimeExtensions.WorkingDays;
using DateTimeExtensions.WorkingDays.CultureStrategies;
using NUnit.Framework;
Expand All @@ -28,7 +26,7 @@ public void assert_holidays_count()
}

[Test]
public void assert_holidays_on_weekend_observed_on_monday()
public void assert_holidays_on_sunday_observed_on_monday()
{
var dateTimeCulture = new WorkingDayCultureInfo("fr-CA");
// 01-07-2012 Canada Day on a Sunday
Expand All @@ -37,6 +35,16 @@ public void assert_holidays_on_weekend_observed_on_monday()
Assert.IsFalse(mondayAfterCanadaDay.IsWorkingDay(dateTimeCulture));
}

[Test]
public void assert_holidays_on_saturday_observed_on_friday()
{
var dateTimeCulture = new WorkingDayCultureInfo("fr-CA");
// 25-12-2010 Christmas on a Saturday
var fridayBeforeCanadaDay = new DateTime(2010, 12, 24);
Assert.AreEqual(DayOfWeek.Friday, fridayBeforeCanadaDay.DayOfWeek);
Assert.IsFalse(fridayBeforeCanadaDay.IsWorkingDay(dateTimeCulture));
}

[Test]
public void CanWorkOn2016()
{
Expand Down
Loading