WorkTimeCalculator 1.0.5

Install-Package WorkTimeCalculator -Version 1.0.5
dotnet add package WorkTimeCalculator --version 1.0.5
<PackageReference Include="WorkTimeCalculator" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WorkTimeCalculator --version 1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: WorkTimeCalculator, 1.0.5"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install WorkTimeCalculator as a Cake Addin
#addin nuget:?package=WorkTimeCalculator&version=1.0.5

// Install WorkTimeCalculator as a Cake Tool
#tool nuget:?package=WorkTimeCalculator&version=1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

WorkTimeCalculator

A utility library to calculate work time within a period, with a configurable work schedule and holidays.

Support

This project is built using C# .Net Core 3.1 class library but it can be ported and compiled with C# .Net

The package is available for multiple frameworks as a NuGet on NuGet.org and you can also use the following Package Manager command to add it to your project

Install-Package WorkTimeCalculator

How To Use

After importing the library in the dependencies of your project, create an instance of the calculator with the work schedule and holidays configuration

var calculator = new WorkTimeCalculator(MySchedule, MyHolidays);

You can also change the configuration if needed by calling SetConfigurations

calculator.SetConfigurations(MySchedule, MyHolidays);

Then call the CalculateWorkTime function

TimeSpan result = calculator.CalculateWorkTime(
  new DateTime(2021, 6, 1, 10, 30, 0), 
  new DateTime(2021, 6, 10, 15, 30, 0),
  true //Optional bool parameter to exclude holidays from calculation [default: true]
);

Work Schedule Configuration Example

Dictionary<DayOfWeek, List<WorkShift>> MySchedule = new Dictionary<DayOfWeek, List<WorkShift>>() {
  { DayOfWeek.Sunday, new List<WorkShift>(){
    new WorkShift(){ Start = new TimeSpan(9,0,0), End = new TimeSpan(13,0,0)},
    new WorkShift(){ Start = new TimeSpan(14,0,0), End = new TimeSpan(18,0,0)}
  } },
  { DayOfWeek.Monday, new List<WorkShift>(){
    new WorkShift(){ Start = new TimeSpan(9,0,0), End = new TimeSpan(13,0,0)},
    new WorkShift(){ Start = new TimeSpan(14,0,0), End = new TimeSpan(18,0,0)}
  } },
  { DayOfWeek.Tuesday, new List<WorkShift>(){
    new WorkShift(){ Start = new TimeSpan(9,0,0), End = new TimeSpan(13,0,0)},
    new WorkShift(){ Start = new TimeSpan(14,0,0), End = new TimeSpan(18,0,0)}
  } },
  { DayOfWeek.Wednesday, new List<WorkShift>(){
    new WorkShift(){ Start = new TimeSpan(9,0,0), End = new TimeSpan(13,0,0)},
    new WorkShift(){ Start = new TimeSpan(14,0,0), End = new TimeSpan(18,0,0)}
  } },
  { DayOfWeek.Thursday, new List<WorkShift>(){
    new WorkShift(){ Start = new TimeSpan(9,0,0), End = new TimeSpan(13,0,0)},
    new WorkShift(){ Start = new TimeSpan(14,0,0), End = new TimeSpan(18,0,0)}
  } }
};

Holidays Configuration Example

List<HolidayConfig> MyHolidays = new List<HolidayConfig>() {
  new HolidayConfig (){ Start = new DateTime(2021, 6, 2), End = new DateTime(2021, 6, 8) },
  new HolidayConfig (){ Start = new DateTime(2021, 7, 2), End = new DateTime(2021, 7, 8) },
  new HolidayConfig (){ Start = new DateTime(2021, 8, 2), End = new DateTime(2021, 8, 8) }
};

Notes

  • The returned type is TimeSpan so the user can extract the value in any time format as needed (hours, minutes, seconds ...etc)
  • The accuracy of the calculated time is in seconds
  • Holidays date configuration is all inclusive of the full days
  • If a day was not configured in the schedule or it has no shifts (or shifts total time value is 0) then it is a non working day
  • Configuration is not checked for validity and it is the responsilbility of the developer, if there are intersections between shifts or holidays it can lead to wrong results
  • The performance of this utility is almost O(n*k) where n is the maximum number of shifts per day, and k is the number of holidays within a single period
  • .NETCoreApp 2.0

    • No dependencies.
  • .NETCoreApp 2.2

    • No dependencies.
  • .NETCoreApp 3.1

    • No dependencies.
  • .NETFramework 4.5

    • No dependencies.
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETStandard 1.0

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
1.0.5 90 8/24/2021
1.0.4.2 73 8/23/2021
1.0.4.1 86 8/23/2021
1.0.4 87 8/23/2021
1.0.3 115 8/19/2021
1.0.2 139 8/18/2021
1.0.1 119 8/17/2021
1.0.0 118 8/17/2021

Description added to functions (enhanced)