WorkTimeCalculator 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package WorkTimeCalculator --version 1.0.0
NuGet\Install-Package WorkTimeCalculator -Version 1.0.0
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="WorkTimeCalculator" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WorkTimeCalculator --version 1.0.0
#r "nuget: WorkTimeCalculator, 1.0.0"
#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 WorkTimeCalculator as a Cake Addin
#addin nuget:?package=WorkTimeCalculator&version=1.0.0

// Install WorkTimeCalculator as a Cake Tool
#tool nuget:?package=WorkTimeCalculator&version=1.0.0

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

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
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 netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

    • No dependencies.

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 115,579 8/24/2021
1.0.4.2 311 8/23/2021
1.0.4.1 302 8/23/2021
1.0.4 1,779 8/23/2021
1.0.3 340 8/19/2021
1.0.2 365 8/18/2021
1.0.1 355 8/17/2021
1.0.0 358 8/17/2021