CSharpEssentials.Time 3.0.1

dotnet add package CSharpEssentials.Time --version 3.0.1
                    
NuGet\Install-Package CSharpEssentials.Time -Version 3.0.1
                    
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="CSharpEssentials.Time" Version="3.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharpEssentials.Time" Version="3.0.1" />
                    
Directory.Packages.props
<PackageReference Include="CSharpEssentials.Time" />
                    
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 CSharpEssentials.Time --version 3.0.1
                    
#r "nuget: CSharpEssentials.Time, 3.0.1"
                    
#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 CSharpEssentials.Time@3.0.1
                    
#: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=CSharpEssentials.Time&version=3.0.1
                    
Install as a Cake Addin
#tool nuget:?package=CSharpEssentials.Time&version=3.0.1
                    
Install as a Cake Tool

CSharpEssentials.Time

CSharpEssentials.Time provides a lightweight, testable abstraction over system time, built on top of the modern .NET TimeProvider. It simplifies date and time operations and makes unit testing time-dependent logic effortless.

๐Ÿš€ Features

  • Testable Time Abstraction: IDateTimeProvider interface to decouple your code from the system clock.
  • Modern .NET Support: Uses DateTimeOffset, DateOnly, and TimeOnly.
  • Simple Extensions: Helper methods to convert DateTime to DateOnly or TimeOnly.
  • TimeProvider Integration: Designed to work seamlessly with the built-in .NET TimeProvider.

๐Ÿ“ฆ Installation

dotnet add package CSharpEssentials.Time

๐Ÿ›  Usage

1. Using the Date Time Provider

Inject IDateTimeProvider into your services to access current time properties. This abstraction allows you to control time during testing.

public class TicketService(IDateTimeProvider dateTimeProvider)
{
    public void ProcessTicket(Ticket ticket)
    {
        // Get current UTC time
        DateTimeOffset now = dateTimeProvider.UtcNow;
        
        // Get just the date part
        DateOnly today = dateTimeProvider.UtcNowDate;

        ticket.ProcessedAt = now;
    }
}

2. Registration

Register the provider in your Dependency Injection container. Since DateTimeProvider depends on .NET's abstract TimeProvider, you should register that as well (usually TimeProvider.System).

using CSharpEssentials.Time;

builder.Services.AddSingleton<TimeProvider>(TimeProvider.System);
builder.Services.AddSingleton<IDateTimeProvider, DateTimeProvider>();

3. Extensions

Convenient extension methods for converting DateTime.

using CSharpEssentials.Time;

DateTime now = DateTime.UtcNow;

DateOnly date = now.ToDateOnly();
TimeOnly time = now.ToTimeOnly();

๐Ÿงช Testing

The main benefit of this package is testability. You can mock IDateTimeProvider to return fixed times, ensuring your tests are deterministic.

// Example using NSubstitute or Moq
var mockTime = Substitute.For<IDateTimeProvider>();
var fixedTime = new DateTimeOffset(2023, 1, 1, 12, 0, 0, TimeSpan.Zero);

mockTime.UtcNow.Returns(fixedTime);
mockTime.UtcNowDate.Returns(DateOnly.FromDateTime(fixedTime.DateTime));

var service = new TicketService(mockTime);
// ... run assertions ...
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.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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 (2)

Showing the top 2 NuGet packages that depend on CSharpEssentials.Time:

Package Downloads
CSharpEssentials

A comprehensive C# library enhancing functional programming capabilities with type-safe monads (Maybe, Result), discriminated unions (Any), and robust error handling. Features include: domain-driven design support, enhanced Entity Framework integration, testable time management, JSON utilities, and LINQ extensions. Built for modern C# development with focus on maintainability, testability, and functional programming principles.

CSharpEssentials.Entity

Domain-driven design (DDD) entity base classes and patterns for CSharpEssentials. Provides EntityBase<TId> with soft delete functionality, audit trails, and domain event integration. Essential for building robust domain models with type safety and functional programming principles.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.1 30 5/3/2026
3.0.0 30 5/3/2026
2.1.0 298 11/26/2025
2.0.9 260 9/30/2025
2.0.8 244 9/29/2025
2.0.7 254 9/29/2025
2.0.6 246 9/29/2025
2.0.5 241 9/29/2025
2.0.4 252 9/28/2025
2.0.3 238 9/28/2025
2.0.2 235 9/28/2025
2.0.1 252 9/28/2025
2.0.0 263 9/28/2025