CSharpEssentials.Time
3.0.1
dotnet add package CSharpEssentials.Time --version 3.0.1
NuGet\Install-Package CSharpEssentials.Time -Version 3.0.1
<PackageReference Include="CSharpEssentials.Time" Version="3.0.1" />
<PackageVersion Include="CSharpEssentials.Time" Version="3.0.1" />
<PackageReference Include="CSharpEssentials.Time" />
paket add CSharpEssentials.Time --version 3.0.1
#r "nuget: CSharpEssentials.Time, 3.0.1"
#:package CSharpEssentials.Time@3.0.1
#addin nuget:?package=CSharpEssentials.Time&version=3.0.1
#tool nuget:?package=CSharpEssentials.Time&version=3.0.1
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:
IDateTimeProviderinterface to decouple your code from the system clock. - Modern .NET Support: Uses
DateTimeOffset,DateOnly, andTimeOnly. - Simple Extensions: Helper methods to convert
DateTimetoDateOnlyorTimeOnly. - 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 | Versions 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. |
-
.NETStandard 2.1
- Microsoft.Bcl.TimeProvider (>= 8.0.0)
-
net9.0
- No dependencies.
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.