PH.WorkingDaysAndTime 2.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package PH.WorkingDaysAndTime --version 2.0.3                
NuGet\Install-Package PH.WorkingDaysAndTime -Version 2.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PH.WorkingDaysAndTime" Version="2.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PH.WorkingDaysAndTime --version 2.0.3                
#r "nuget: PH.WorkingDaysAndTime, 2.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PH.WorkingDaysAndTime as a Cake Addin
#addin nuget:?package=PH.WorkingDaysAndTime&version=2.0.3

// Install PH.WorkingDaysAndTime as a Cake Tool
#tool nuget:?package=PH.WorkingDaysAndTime&version=2.0.3                

A tiny c# utility for calculating work days and work time. The code is written in .NET C#.

The tool is useful for calculate difference between two dates of workdays, to plan projects excluding holidays and absences. Is also a simple starting-point to addons to estimate the date of the end of a job.

The application works only counting the dates forward and it is assumed that the date entered as the first parameter is a working day.

Features

  • can add n work-days to a DateTime;
  • can add n work-hours to a DateTime;
  • can get a List of work-DateTime between 2 dates;

Code Examples

AddWorkingDays(DateTime start, int days)

//this is the configuration of a work-week: 8h/day from monday to friday
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}}
	}
};

//this is the configuration for holidays: 
//in Italy we have this list of Holidays plus 1 day different on each province,
//for mine is 1 Dec (see last element of the List<AHolyDay>).
var italiansHoliDays = new List<AHolyDay>()
{
	new EasterMonday(),new HoliDay(1, 1),new HoliDay(6, 1),
	new HoliDay(25, 4),new HoliDay(1, 5),new HoliDay(2, 6),
	new HoliDay(15, 8),new HoliDay(1, 11),new HoliDay(8, 12),
	new HoliDay(25, 12),new HoliDay(26, 12)
	, new HoliDay(1, 12)
};

//instantiate with configuration
var utility = new WorkingDaysAndTimeUtility(week, italiansHoliDays);

//lets-go: add 3 working-days to Jun 1, 2015
var result = utility.AddWorkingDays(new DateTime(2015,6,1), 3);
//result is Jun 5, 2015 (see holidays list) 

GetWorkingDaysBetweenTwoDateTimes(DateTime start, DateTime end, bool includeStartAndEnd = true)

var start = new DateTime(2015, 12, 31, 9, 0, 0);
var end = new DateTime(2016, 1, 7, 9, 0, 0);

//omitted configurations and holidays...
var utility = new WorkingDaysAndTimeUtility(weekConf, GetItalianHolidays());

//r is a workdays List<DateTime> between Dec 31 and Jan 7.
var r = utility.GetWorkingDaysBetweenTwoDateTimes(start, end);

Testing if given date is Working-Datetime

[Fact]
public void Get_IfWorkingDay_OnTuesday_OnSimpleWeek_ReturnTrue()
{

    //omitted configurations and holidays...
    var tuesday = new DateTime(2018, 11, 6, 11,22,33);
    var prev0 = tuesday.AddMinutes(-1);
    var next0 = tuesday.AddMinutes(1);

    var weekConf = GetSimpleWeek();
    var utility  = new WorkingDaysAndTimeUtility(weekConf, new List<HoliDay>());

    var r = utility.IfWorkingMoment(tuesday, out DateTime next, out DateTime previous);

    Assert.True(r);
    Assert.Equal(prev0, previous);
    Assert.Equal(next0, next);

}

Code Configuration Examples

Use of WorkingDaysConfig

//note thats w is WeekDaySpan and l is List<AHolyDay>
//cfg is Json serializable
var cfg = new WorkingDaysConfig(w, l);

Map-Config Style

var cfg = new WorkingDaysConfig()
                      .Week(new WeekDaySpan().Day(DayOfWeek.Monday,
                                                  new WorkDaySpan()
                                                      .Time(new TimeSpan(9, 0, 0), new TimeSpan(13, 0, 0))
                                                      .Time(new TimeSpan(14, 0, 0), new TimeSpan(18, 0, 0)))
                                             .Day(DayOfWeek.Tuesday,
                                                  new WorkDaySpan()
                                                      .Time(new TimeSpan(9, 0, 0), new TimeSpan(13, 0, 0))
                                                      .Time(new TimeSpan(14, 0, 0), new TimeSpan(18, 0, 0)))
                           )
                      .Holiday(new AHolyDay(15, 8))
                      .Holiday(2, 6)
                      .Holiday(new EasterMonday());

var cfg2 = new WorkingDaysConfig().Week(WeekDaySpan.CreateSymmetricalConfig(new WorkDaySpan()
																			.Time(new TimeSpan(9, 0, 0),
																					new TimeSpan(13, 0, 0))
																			.Time(new TimeSpan(14, 0, 0),
																					new TimeSpan(18, 0, 0)),
																			new DayOfWeek[]
																			{
																				DayOfWeek.Monday,
																				DayOfWeek.Tuesday,
																				DayOfWeek.Wednesday,
																				DayOfWeek.Thursday,
																				DayOfWeek.Friday
																			}));

License

This software is licensed by BSD-3-Clause. Link to License: license

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.13 1,102 5/9/2023
2.0.12 358 1/19/2023
2.0.11 4,766 6/27/2022
2.0.10 3,407 8/6/2021
2.0.9 370 8/2/2021
2.0.8 353 7/14/2021
2.0.7 355 7/13/2021
2.0.6 11,277 3/30/2021
2.0.5 12,482 6/29/2020
2.0.4 12,423 11/15/2018
2.0.3 682 11/15/2018
2.0.2 693 11/15/2018
2.0.1 693 11/14/2018
2.0.0 705 11/13/2018

Fix errors on serializing and de-serializing List of AHolyDay