CSharpEssentials.Time
6.5.2
dotnet add package CSharpEssentials.Time --version 6.5.2
NuGet\Install-Package CSharpEssentials.Time -Version 6.5.2
<PackageReference Include="CSharpEssentials.Time" Version="6.5.2" />
<PackageVersion Include="CSharpEssentials.Time" Version="6.5.2" />
<PackageReference Include="CSharpEssentials.Time" />
paket add CSharpEssentials.Time --version 6.5.2
#r "nuget: CSharpEssentials.Time, 6.5.2"
#:package CSharpEssentials.Time@6.5.2
#addin nuget:?package=CSharpEssentials.Time&version=6.5.2
#tool nuget:?package=CSharpEssentials.Time&version=6.5.2
CSharpEssentials.Time
Testable time abstraction built on .NET TimeProvider. Decouple your code from the system clock for reliable unit testing.
Features
- IDateTimeProvider: Injectable interface for
UtcNow,UtcNowDateTime(alwaysDateTimeKind.Utc),UtcNowDate,UtcNowTime,TimeZoneandTimeZoneUtc. - TimeProvider Integration:
DateTimeProviderwraps .NET'sTimeProvider, soTimeProvider.Systemor a testTimeProvidersuch asFakeTimeProviderboth work. - FakeDateTimeProvider: Built-in controllable clock with
AdvanceandSetTimefor tests. - DateTime Extensions: Convert
DateTimetoDateOnlyandTimeOnly, next/previous weekday, age calculation.
Installation
dotnet add package CSharpEssentials.Time
Targets net11.0, net10.0, net9.0 and netstandard2.1 (which adds Microsoft.Bcl.TimeProvider).
Usage
DateTimeProvider
using CSharpEssentials.Time;
public class OrderService(IDateTimeProvider time)
{
public Order CreateOrder()
{
return new Order
{
CreatedAt = time.UtcNow,
CreatedDate = time.UtcNowDate
};
}
}
DateTimeProvider.UtcNow reads TimeProvider.GetUtcNow(). UtcNowDate and UtcNowTime are the UTC date and time of day. TimeZone is TimeZoneInfo.Local and TimeZoneUtc is TimeZoneInfo.Utc.
Dependency Injection
builder.Services.AddSingleton<TimeProvider>(TimeProvider.System);
builder.Services.AddSingleton<IDateTimeProvider, DateTimeProvider>();
Test with FakeDateTimeProvider
CSharpEssentials.Time ships a built-in FakeDateTimeProvider, so no extra package is needed. Its TimeZone is UTC. The sample below asserts with FluentAssertions, which is not a dependency of this package.
using CSharpEssentials.Time;
using FluentAssertions;
var fixedTime = new DateTimeOffset(2025, 1, 15, 10, 0, 0, TimeSpan.Zero);
var fake = new FakeDateTimeProvider(fixedTime);
var svc = new OrderService(fake);
Order order = svc.CreateOrder();
order.CreatedAt.Should().Be(fixedTime);
// Advance the clock — no Thread.Sleep needed
fake.Advance(TimeSpan.FromHours(2));
fake.UtcNow.Should().Be(fixedTime.AddHours(2));
// Set to a specific time
fake.SetTime(new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero));
// .NET 6+ targets only (not netstandard2.1)
fake.UtcNowDate.Should().Be(new DateOnly(2026, 1, 1));
fake.UtcNowTime.Should().Be(new TimeOnly(0, 0, 0));
Extensions
DateTime now = DateTime.UtcNow;
DateOnly date = now.ToDateOnly();
TimeOnly time = now.ToTimeOnly();
DateOnly today = new(2024, 6, 15); // Saturday
today.NextDayOfWeek(DayOfWeek.Monday); // 2024-06-17
today.PreviousDayOfWeek(DayOfWeek.Saturday); // 2024-06-08
today.NextDayOfWeek(DayOfWeek.Saturday, includeCurrent: true); // 2024-06-15
new DateOnly(2000, 6, 16).GetAge(today); // 23
birthDate.GetAge(dateTimeProvider); // uses the provider's time zone
NextDayOfWeek and PreviousDayOfWeek skip the current day unless includeCurrent: true. GetAge counts whole years and throws ArgumentOutOfRangeException when the birth date is after the reference date. DateOnly and TimeOnly members and GetAge require .NET 6+; on netstandard2.1 only the DateTime weekday overloads are available.
| 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 is compatible. 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. net11.0 is compatible. |
| .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)
-
net10.0
- No dependencies.
-
net11.0
- No dependencies.
-
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.
| Version | Downloads | Last Updated |
|---|---|---|
| 6.5.2 | 145 | 10/10/2026 |
| 6.5.1 | 145 | 10/10/2026 |
| 6.5.0 | 142 | 10/9/2026 |
| 6.4.0 | 136 | 10/9/2026 |
| 6.3.0 | 144 | 10/8/2026 |
| 6.2.0 | 156 | 10/8/2026 |
| 6.1.0 | 156 | 10/7/2026 |
| 6.0.0 | 161 | 10/7/2026 |
| 5.2.1 | 165 | 10/7/2026 |
| 5.2.0 | 155 | 10/7/2026 |
| 5.1.0 | 160 | 10/7/2026 |
| 5.0.0 | 161 | 10/6/2026 |
| 4.1.0 | 160 | 10/6/2026 |
| 4.0.0 | 156 | 10/5/2026 |
| 3.2.3 | 338 | 5/31/2026 |
| 3.2.2 | 207 | 5/31/2026 |
| 3.2.1 | 207 | 5/31/2026 |
| 3.2.0 | 207 | 5/30/2026 |
| 3.1.0 | 215 | 5/27/2026 |
| 3.0.8 | 206 | 5/20/2026 |