CG.Infrastructure.Calendar 3.10.2

dotnet add package CG.Infrastructure.Calendar --version 3.10.2
                    
NuGet\Install-Package CG.Infrastructure.Calendar -Version 3.10.2
                    
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="CG.Infrastructure.Calendar" Version="3.10.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CG.Infrastructure.Calendar" Version="3.10.2" />
                    
Directory.Packages.props
<PackageReference Include="CG.Infrastructure.Calendar" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CG.Infrastructure.Calendar --version 3.10.2
                    
#r "nuget: CG.Infrastructure.Calendar, 3.10.2"
                    
#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.
#:package CG.Infrastructure.Calendar@3.10.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CG.Infrastructure.Calendar&version=3.10.2
                    
Install as a Cake Addin
#tool nuget:?package=CG.Infrastructure.Calendar&version=3.10.2
                    
Install as a Cake Tool

CG.Infrastructure.Calendar

NuGet .NET

A comprehensive calendar and time management library that provides business logic for working with dates, times, work schedules, and holiday calculations in .NET applications.

Overview

CG.Infrastructure.Calendar is a specialized library that extends the core infrastructure with comprehensive calendar functionality. It provides business logic for working with work schedules, holiday calculations, time tracking, and various calendar operations commonly needed in business applications.

Features

  • Date and Time Utilities: Comprehensive date manipulation and calculation methods
  • Work Schedule Management: Calculate working hours, lunch breaks, and time tracking
  • Holiday Calculations: UK bank holiday calculations with automatic weekend adjustments
  • Tax Year Support: UK tax year and period calculations
  • Day Type Classification: Support for various day types (weekday, weekend, bank holiday, leave, etc.)
  • Time Zone Support: Time zone aware operations
  • Flexible Time Calculations: Calculate flexi time, hours worked, and time remaining
  • Configuration Integration: Seamless integration with .NET configuration system

Requirements

  • .NET 9.0 or later
  • CG.Infrastructure.Core 3.10.4+
  • Microsoft.Extensions.Configuration.Abstractions 9.0.7+
  • Microsoft.Extensions.Configuration.Binder 9.0.7+

Installation

dotnet add package CG.Infrastructure.Calendar

Or add the following to your .csproj file:

<PackageReference Include="CG.Infrastructure.Calendar" Version="3.10.0" />

Quick Start

1. Configure Calendar Options

Add configuration to your appsettings.json:

{
  "CalendarOptions": {
    "StartDate": "2024-01-01",
    "WorkingHours": "08:00",
    "LunchBreak": "01:00"
  }
}

2. Register Services

In your Program.cs or Startup.cs:

using Infrastructure.Calendar.Extensions;
using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// Register calendar services with configuration
builder.Services.RegisterInfraCalendarServices(builder.Configuration);

var app = builder.Build();

3. Use the Calendar Service

[ApiController]
[Route("[controller]")]
public class TimeTrackingController : ControllerBase
{
    private readonly ICalendarService _calendarService;

    public TimeTrackingController(ICalendarService calendarService)
    {
        _calendarService = calendarService;
    }

    [HttpGet("work-hours")]
    public ActionResult<TimeSpan> GetWorkHours(DateTime start, DateTime lunch, DateTime back)
    {
        var startOffset = _calendarService.GetDateTimeOffset(start);
        var lunchOffset = _calendarService.GetDateTimeOffset(lunch);
        var backOffset = _calendarService.GetDateTimeOffset(back);

        var hoursWorked = _calendarService.GetWorkedToday(startOffset, lunchOffset, backOffset);
        return Ok(hoursWorked);
    }
}

Core Functionality

Date and Time Operations

// Get start and end of week
var startOfWeek = _calendarService.GetStartOfWeek(DateTimeOffset.Now);
var endOfWeek = _calendarService.GetEndOfWeek(DateTimeOffset.Now);

// Get end of month
var endOfMonth = _calendarService.GetEndOfMonth(DateTimeOffset.Now);

// Get week number
var weekNumber = _calendarService.GetWeekNumber(DateTimeOffset.Now, CalendarWeekRule.FirstFullWeek);

Tax Year Calculations

// Get tax year (UK tax year starts April 6th)
var taxYear = _calendarService.GetTaxYearFromDate(DateTimeOffset.Now);

// Get tax period (1-12, where 1 = April)
var taxPeriod = _calendarService.GetTaxPeriodFromDate(DateTimeOffset.Now);

Work Schedule Management

// Calculate hours worked today
var hoursWorked = _calendarService.GetWorkedToday(start, lunch, back);

// Calculate remaining time today
var timeLeft = _calendarService.GetLeftToday(hoursWorked, expectedHours);

// Calculate finish time
var finishTime = _calendarService.GetFinishToday(start, lunch, back, finish, worked, left);

// Calculate lunch break duration
var lunchDuration = _calendarService.GetLunchBreak(start, finish);

Day Type Classification

// Determine day type
var dayType = _calendarService.GetDayType(DateTimeOffset.Now);

// Get expected hours for day type
var expectedHours = _calendarService.GetDayTypeHours(dayType);

// Calculate days worked based on day type
var daysWorked = _calendarService.GetDaysWorked(dayType);

Holiday and Leave Management

// Calculate holiday taken (full and half days)
var holidayTaken = _calendarService.GetHolidayTaken(fullDays, halfDays);

// Get UK bank holidays for a year
var holidays = CalendarService.GetHolidays(2024);

// Check if date is a bank holiday
var isBankHoliday = _calendarService.GetDayType(date) == DayTypeEnum.BankHoliday;

Time Zone Operations

// Get midnight in specific time zone
var midnightInTz = _calendarService.MidnightInTimeZone(date, "Europe/London");

// Convert DateTime to DateTimeOffset
var dateTimeOffset = _calendarService.GetDateTimeOffset(dateTime);

// Parse date from string
var parsedDate = _calendarService.GetDateFromString("2024-01-01");

Flexi Time Calculations

// Calculate flexi time (difference between worked and expected)
var flexiTime = _calendarService.GetFlexi(hoursWorked, expectedHours);

// Generate time list for scheduling
var timeSlots = _calendarService.GetTimeList(date, 30); // 30-minute intervals

Configuration Options

The library supports configuration through CalendarOptions:

Property Description Default
StartDate The start date for calculations Empty string
WorkingHours Standard working hours per day Empty string
LunchBreak Standard lunch break duration Empty string

Day Types

The library supports various day types through DayTypeEnum:

  • Weekday: Standard working day
  • Weekend: Saturday and Sunday
  • BankHoliday: UK bank holidays
  • AnnualLeave: Annual leave days
  • HalfDay: Half working day
  • Training: Training days
  • Sick: Sick leave days
  • CompassionateLeave: Compassionate leave
  • PaternityLeave: Paternity leave
  • Special: Special working days
  • Toil: Time off in lieu
  • NonWorking: Non-working days
  • WellBeing: Well-being days
  • Giving: Charity/volunteer days

Dependencies

  • CG.Infrastructure.Core: Provides the base service infrastructure and dependency injection support
  • Microsoft.Extensions.Configuration.Abstractions: Configuration system integration
  • Microsoft.Extensions.Configuration.Binder: Configuration binding support

Integration with Core Infrastructure

This library extends the core infrastructure by:

  1. Implementing IService: Integrates with the automatic service registration system
  2. Configuration Binding: Automatically binds configuration sections to options
  3. Dependency Injection: Seamlessly integrates with the DI container
  4. Service Extensions: Provides extension methods for easy service registration

Contributing

This library is part of the CG Infrastructure suite. For contributions, please follow the established patterns and ensure all tests pass.

License

This project is licensed under the terms specified in the LICENSE file.

Version History

  • 3.10.0: Current stable release
  • Supports .NET 9.0
  • Includes comprehensive calendar and time management capabilities
  • Provides UK-specific business logic for tax years and bank holidays
  • Integrates with the core infrastructure service registration system

Support

For issues, questions, or contributions, please refer to the project repository or contact the maintainers.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows 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
3.10.2 140 8/15/2025
3.10.1 154 8/10/2025
3.10.0 549 7/22/2025
3.9.0 151 12/10/2024
2.0.7 180 10/3/2023
2.0.6 168 9/27/2023
2.0.5 176 9/27/2023
2.0.4 170 9/25/2023
2.0.3 234 7/17/2023
2.0.2 201 7/14/2023
2.0.1 224 7/14/2023
2.0.0 232 7/12/2023
1.0.0 310 5/26/2022