CSharpEssentials.Time
9.0.0
dotnet add package CSharpEssentials.Time --version 9.0.0
NuGet\Install-Package CSharpEssentials.Time -Version 9.0.0
<PackageReference Include="CSharpEssentials.Time" Version="9.0.0" />
<PackageVersion Include="CSharpEssentials.Time" Version="9.0.0" />
<PackageReference Include="CSharpEssentials.Time" />
paket add CSharpEssentials.Time --version 9.0.0
#r "nuget: CSharpEssentials.Time, 9.0.0"
#:package CSharpEssentials.Time@9.0.0
#addin nuget:?package=CSharpEssentials.Time&version=9.0.0
#tool nuget:?package=CSharpEssentials.Time&version=9.0.0
CSharpEssentials.Time
A testable time abstraction built on .NET TimeProvider. Code that reads the time through IDateTimeProvider does not depend on the system clock, so unit tests can control the time.
Benchmarks of the clock reads (UtcNow, UtcNowDateTime, UtcNowDate, UtcNowTime) against TimeProvider.System and DateTimeOffset.UtcNow, and of the DateOnly, TimeOnly, weekday and age helpers against the BCL and hand-written code, are in BENCHMARKS.md.
Installation
dotnet add package CSharpEssentials.Time
Targets net11.0, net10.0, net9.0 and netstandard2.1 (which adds Microsoft.Bcl.TimeProvider). The DateOnly and TimeOnly members (UtcNowDate, UtcNowTime, ToDateOnly, ToTimeOnly, the DateOnly weekday overloads, TodayIn and GetAge) are not in the netstandard2.1 build.
Usage
IDateTimeProvider and DateTimeProvider
using CSharpEssentials.Time;
public class OrderService(IDateTimeProvider time)
{
public Order CreateOrder() => new()
{
CreatedAt = time.UtcNow,
CreatedDate = time.UtcNowDate
};
}
builder.Services.AddSingleton<TimeProvider>(TimeProvider.System);
builder.Services.AddSingleton<IDateTimeProvider, DateTimeProvider>();
IDateTimeProvider exposes UtcNow, UtcNowDateTime (always DateTimeKind.Utc), UtcNowDate, UtcNowTime, TimeZone and TimeZoneUtc. DateTimeProvider wraps a TimeProvider, so TimeProvider.System or a test provider such as FakeTimeProvider both work. UtcNow reads TimeProvider.GetUtcNow(), UtcNowDate and UtcNowTime are the UTC date and time of day, TimeZone is the LocalTimeZone of the TimeProvider (TimeZoneInfo.Local for TimeProvider.System) and TimeZoneUtc is TimeZoneInfo.Utc.
Testing with a fake clock
In tests, wrap FakeTimeProvider from Microsoft.Extensions.TimeProvider.Testing: new DateTimeProvider(fakeTimeProvider). The same fake then drives IDateTimeProvider, timers, Task.Delay(TimeSpan, TimeProvider) and anything else that takes the TimeProvider, and SetLocalTimeZone sets TimeZone. This is the clock to use when the code under test waits or schedules.
var fakeTime = new FakeTimeProvider(new DateTimeOffset(2025, 1, 15, 10, 0, 0, TimeSpan.Zero));
var time = new DateTimeProvider(fakeTime);
fakeTime.Advance(TimeSpan.FromHours(2)); // time.UtcNow moves, and so do timers on fakeTime
FakeDateTimeProvider
The package also ships FakeDateTimeProvider, a small clock for code that only reads IDateTimeProvider. It starts at a fixed time and moves only with Advance and SetTime. Its TimeZone is UTC, and it is safe to use from several threads. It is not a TimeProvider: timers and delays never see its time, so use the FakeTimeProvider setup above for code that waits. The sample asserts with FluentAssertions, which is not a dependency of this package.
var fixedTime = new DateTimeOffset(2025, 1, 15, 10, 0, 0, TimeSpan.Zero);
var fake = new FakeDateTimeProvider(fixedTime);
Order order = new OrderService(fake).CreateOrder();
order.CreatedAt.Should().Be(fixedTime);
fake.Advance(TimeSpan.FromHours(2));
fake.UtcNow.Should().Be(fixedTime.AddHours(2));
fake.SetTime(new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero));
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); // today in the provider's time zone
Time zones
TimeZones.Find(id) takes an IANA id (Europe/Istanbul) or a Windows id (Turkey Standard Time) on any operating system and throws TimeZoneNotFoundException naming the id when neither is known. TimeZones.TryFind(id, out zone) returns false instead, also for a null or blank id. On .NET 8 and later an id the system does not know directly is converted between IANA and Windows with TimeZoneInfo.TryConvertIanaIdToWindowsId or TryConvertWindowsIdToIanaId and looked up again; that needs ICU, so with InvariantGlobalization only the system's own ids resolve.
NowIn(zone) and TodayIn(zone) give the provider's current time and date in a user's zone, and GetAge(provider, zone) counts the age on that date:
TimeZoneInfo zone = TimeZones.Find(user.TimeZoneId);
DateTimeOffset localNow = time.NowIn(zone); // the provider's UtcNow with the zone's offset
DateOnly today = time.TodayIn(zone); // 2024-06-16 in Istanbul while it is 2024-06-15 22:30 UTC
int age = user.BirthDate.GetAge(time, zone);
NextDayOfWeek and PreviousDayOfWeek exist for DateTime and DateOnly and skip the current day unless includeCurrent: true. GetAge counts whole years and throws ArgumentOutOfRangeException when the birth date is after the reference date.
| 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 |
|---|---|---|
| 9.0.0 | 0 | 11/28/2026 |
| 8.0.0 | 270 | 11/9/2026 |
| 7.0.0 | 253 | 10/20/2026 |
| 6.5.2 | 196 | 10/10/2026 |
| 6.5.1 | 189 | 10/10/2026 |
| 6.5.0 | 189 | 10/9/2026 |
| 6.4.0 | 181 | 10/9/2026 |
| 6.3.0 | 188 | 10/8/2026 |
| 6.2.0 | 188 | 10/8/2026 |
| 6.1.0 | 190 | 10/7/2026 |
| 6.0.0 | 190 | 10/7/2026 |
| 5.2.1 | 193 | 10/7/2026 |
| 5.2.0 | 190 | 10/7/2026 |
| 5.1.0 | 192 | 10/7/2026 |
| 5.0.0 | 190 | 10/6/2026 |
| 4.1.0 | 196 | 10/6/2026 |
| 4.0.0 | 185 | 10/5/2026 |
| 3.2.3 | 407 | 5/31/2026 |
| 3.2.2 | 224 | 5/31/2026 |
| 3.2.1 | 228 | 5/31/2026 |