Skip to content

Commit

Permalink
Add Multi-Calculated holiday
Browse files Browse the repository at this point in the history
  • Loading branch information
paonath committed Aug 2, 2021
1 parent 1c89359 commit 2ad0e9f
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 8 deletions.
6 changes: 3 additions & 3 deletions PH.WorkingDaysAndTime/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<PropertyGroup>
<Authors>Paolo Innocenti</Authors>
<Copyright>Copyright 2021 (c) Paolo Innocenti - [email protected] </Copyright>
<AssemblyVersion>2.0.8</AssemblyVersion>
<FileVersion>2.0.8</FileVersion>
<Version>2.0.8</Version>
<AssemblyVersion>2.0.9</AssemblyVersion>
<FileVersion>2.0.9</FileVersion>
<Version>2.0.9</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using PH.WorkingDaysAndTimeUtility.Configuration;
using Xunit;

namespace PH.WorkingDaysAndTimeUtility.UnitTest
{
public class MultiCalculatedHolidaysTest : BaseTest
{
[Fact]
public void TestWorkingOnSaturdayIfOdd()
{
var wts1 = new WorkTimeSpan() { Start = new TimeSpan(9, 0, 0), End = new TimeSpan(13, 0, 0) };

var wts2 = new WorkTimeSpan() { Start = new TimeSpan(14, 0, 0), End = new TimeSpan(18, 0, 0) };
var wts = new List<WorkTimeSpan>() { wts1, wts2 };
var week = new WeekDaySpan()
{
WorkDays = new Dictionary<DayOfWeek, WorkDaySpan>()
{
{DayOfWeek.Monday, new WorkDaySpan() {TimeSpans = wts }}
,
{DayOfWeek.Tuesday, new WorkDaySpan() {TimeSpans = wts}}
,
{DayOfWeek.Wednesday, new WorkDaySpan() {TimeSpans = wts}}
,
{DayOfWeek.Thursday, new WorkDaySpan() {TimeSpans = wts}}
,
{DayOfWeek.Friday, new WorkDaySpan() {TimeSpans = wts}} ,
{DayOfWeek.Saturday, new WorkDaySpan() {TimeSpans = wts}}
}
};

var italians = GetItalianHolidaysWithNoEasterMonday();
italians.Add(new EasterMonday());
italians.Add(new HoliDay(1, 12));
italians.Add(new WorkingOnSaturdayIfOdd());

var utility = new PH.WorkingDaysAndTimeUtility.WorkingDaysAndTimeUtility(week, italians);

var aSaturday = new DateTime(2021, 8, 7);
var t1 = utility.IsAWorkDay(aSaturday);
var t2 = utility.IsAWorkDay(aSaturday.AddDays(7));
var t3 = utility.IsAWorkDay(aSaturday.AddDays(14));
var t4 = utility.IsAWorkDay(aSaturday.AddDays(21));

Assert.False(t1);
Assert.False(t3);
Assert.True(t2);
Assert.True(t4);

}
}

public class WorkingOnSaturdayIfOdd : MultiCalculatedHoliDay
{
public WorkingOnSaturdayIfOdd() : base(0, 0)
{
}

public override Type GetHolyDayType() => typeof(WorkingOnSaturdayIfOdd);

/// <summary>Calculates the list of MultiCalculatedHoliDays for the given year.</summary>
/// <param name="year">The year.</param>
/// <returns></returns>
public override List<DateTime> CalculateList(int year)
{
var first = new DateTime(year, 1, 1, 0, 0, 0);
var last = new DateTime(year +1, 1, 1, 0, 0, 0);
CultureInfo myCI = new CultureInfo("it-IT");
var baseHolidays = new List<DateTime>();
while (first < last)
{
if (first.DayOfWeek == DayOfWeek.Saturday)
{
var weekNumber = myCI.Calendar.GetWeekOfYear(first, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
if (weekNumber % 2 == 0)
{
baseHolidays.Add(first);
}
}

first = first.AddDays(1);
}

return baseHolidays;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using PH.WorkingDaysAndTimeUtility.Converter;

Expand Down Expand Up @@ -150,6 +151,33 @@ public string AHolyDayToString()
}
}

public abstract class MultiCalculatedHoliDay : CalculatedHoliDay
{

/// <summary>
/// It returns an instance of the data by year provided
/// </summary>
/// <param name="year">year provided</param>
/// <returns>DateTime</returns>
[Obsolete("For MultiCalculatedHoliDay use 'List<DateTime> CalculateList(int year);'", true)]
#pragma warning disable CS0809 // Obsolete member overrides non-obsolete member
public override DateTime Calculate(int year)
#pragma warning restore CS0809 // Obsolete member overrides non-obsolete member
{
throw new
NotSupportedException($"For {nameof(MultiCalculatedHoliDay)} use 'List<DateTime> CalculateList(int year);'");
}

/// <summary>Calculates the list of MultiCalculatedHoliDays for the given year.</summary>
/// <param name="year">The year.</param>
/// <returns></returns>
public abstract List<DateTime> CalculateList(int year);

protected MultiCalculatedHoliDay(int day, int mont) : base(day, mont)
{
}
}

/// <summary>
/// Holiday: a non-working day.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@

The application works only counting the dates forward and it is assumed that the date entered as the first parameter is a working day.</Description>
<PackageReleaseNotes>
Fix time splitting bugs.
Now able to determine if given date is holyday
Now able to add multi-calculated holidays to your configuration
</PackageReleaseNotes>


<PackageTags>work-days,DateTime,work-hours,work-minutes,work-DateTime,holiday,timespan, time-slices</PackageTags>
<PackageTags>work-days,DateTime,work-hours,work-minutes,work-DateTime,holiday,timespan, time-slices, holiday</PackageTags>

<PackageVersion>2.0.8</PackageVersion>
<PackageVersion>2.0.9</PackageVersion>
<Authors>Paolo Innocenti</Authors>
<Copyright>Copyright PH 2021 (c) [email protected]. All rights reserved.</Copyright>
<RepositoryUrl>https://github.com/paonath/PH.WorkingDaysAndTime</RepositoryUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,15 @@ private List<DateTime> CalculateDaysForExclusions(int year)
{
try
{
r.Add(day.Calculate(year));
if (day is MultiCalculatedHoliDay multi)
{
r.AddRange(multi.CalculateList(year));
}
else
{
r.Add(day.Calculate(year));
}

}
catch
{
Expand Down
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,89 @@ var res = utility.SplitWorkedTimeInFactors(n, e);
var hours = res.TotalDuration.TotalHours;
```

**Implements a multi-calculated holiday**
```c#
public class WorkingOnSaturdayIfOdd : MultiCalculatedHoliDay
{
public WorkingOnSaturdayIfOdd() : base(0, 0)
{
}

public override Type GetHolyDayType() => typeof(WorkingOnSaturdayIfOdd);

/// <summary>Calculates the list of MultiCalculatedHoliDays for the given year.</summary>
/// <param name="year">The year.</param>
/// <returns></returns>
public override List<DateTime> CalculateList(int year)
{
var first = new DateTime(year, 1, 1, 0, 0, 0);
var last = new DateTime(year +1, 1, 1, 0, 0, 0);
CultureInfo myCI = new CultureInfo("it-IT");
var baseHolidays = new List<DateTime>();
while (first < last)
{
if (first.DayOfWeek == DayOfWeek.Saturday)
{
var weekNumber = myCI.Calendar.GetWeekOfYear(first, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
if (weekNumber % 2 == 0)
{
baseHolidays.Add(first);
}
}

first = first.AddDays(1);
}

return baseHolidays;
}
}
//...
[Fact]
public void TestWorkingOnSaturdayIfOdd()
{
var wts1 = new WorkTimeSpan() { Start = new TimeSpan(9, 0, 0), End = new TimeSpan(13, 0, 0) };

var wts2 = new WorkTimeSpan() { Start = new TimeSpan(14, 0, 0), End = new TimeSpan(18, 0, 0) };
var wts = new List<WorkTimeSpan>() { wts1, wts2 };
var week = new WeekDaySpan()
{
WorkDays = new Dictionary<DayOfWeek, WorkDaySpan>()
{
{DayOfWeek.Monday, new WorkDaySpan() {TimeSpans = wts }}
,
{DayOfWeek.Tuesday, new WorkDaySpan() {TimeSpans = wts}}
,
{DayOfWeek.Wednesday, new WorkDaySpan() {TimeSpans = wts}}
,
{DayOfWeek.Thursday, new WorkDaySpan() {TimeSpans = wts}}
,
{DayOfWeek.Friday, new WorkDaySpan() {TimeSpans = wts}} ,
{DayOfWeek.Saturday, new WorkDaySpan() {TimeSpans = wts}}
}
};

var italians = GetItalianHolidaysWithNoEasterMonday();
italians.Add(new EasterMonday());
italians.Add(new HoliDay(1, 12));
italians.Add(new WorkingOnSaturdayIfOdd());

var utility = new PH.WorkingDaysAndTimeUtility.WorkingDaysAndTimeUtility(week, italians);

var aSaturday = new DateTime(2021, 8, 7);
var t1 = utility.IsAWorkDay(aSaturday);
var t2 = utility.IsAWorkDay(aSaturday.AddDays(7));
var t3 = utility.IsAWorkDay(aSaturday.AddDays(14));
var t4 = utility.IsAWorkDay(aSaturday.AddDays(21));

Assert.False(t1);
Assert.False(t3);
Assert.True(t2);
Assert.True(t4);

}
}
```

## Code Configuration Examples

**Use of WorkingDaysConfig**
Expand Down

0 comments on commit 2ad0e9f

Please sign in to comment.