Philiprehberger.Clock
0.1.8
dotnet add package Philiprehberger.Clock --version 0.1.8
NuGet\Install-Package Philiprehberger.Clock -Version 0.1.8
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="Philiprehberger.Clock" Version="0.1.8" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Philiprehberger.Clock" Version="0.1.8" />
<PackageReference Include="Philiprehberger.Clock" />
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 Philiprehberger.Clock --version 0.1.8
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Philiprehberger.Clock, 0.1.8"
#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 Philiprehberger.Clock@0.1.8
#: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=Philiprehberger.Clock&version=0.1.8
#tool nuget:?package=Philiprehberger.Clock&version=0.1.8
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Philiprehberger.Clock
Abstraction over DateTime/DateTimeOffset for testable time-dependent code with a fake clock for testing.
Installation
dotnet add package Philiprehberger.Clock
Usage
Inject IClock into your services
using Philiprehberger.Clock;
public class OrderService
{
private readonly IClock _clock;
public OrderService(IClock clock)
{
_clock = clock;
}
public Order PlaceOrder(string item)
{
return new Order(item, _clock.UtcNow);
}
}
Register with dependency injection
using Philiprehberger.Clock;
// Production — uses real system time
builder.Services.AddClock();
// Testing — uses controllable fake time
builder.Services.AddFakeClock(new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero));
Use SystemClock directly
using Philiprehberger.Clock;
var clock = new SystemClock();
Console.WriteLine(clock.UtcNow);
Console.WriteLine(clock.Today);
Use FakeClock in tests
using Philiprehberger.Clock;
var start = new DateTimeOffset(2026, 6, 15, 10, 0, 0, TimeSpan.Zero);
var clock = new FakeClock(start);
// Advance time manually
clock.Advance(TimeSpan.FromHours(2));
Console.WriteLine(clock.UtcNow); // 2026-06-15 12:00:00
// Set exact time
clock.SetTime(new DateTimeOffset(2026, 12, 25, 0, 0, 0, TimeSpan.Zero));
Console.WriteLine(clock.Today); // 2026-12-25
// Auto-advance on each access
clock.AutoAdvance = TimeSpan.FromMinutes(5);
Console.WriteLine(clock.UtcNow); // 2026-12-25 00:00:00
Console.WriteLine(clock.UtcNow); // 2026-12-25 00:05:00
Console.WriteLine(clock.UtcNow); // 2026-12-25 00:10:00
API
IClock
| Member | Type | Description |
|---|---|---|
Now |
DateTimeOffset |
Current local date and time with offset |
UtcNow |
DateTimeOffset |
Current UTC date and time with offset |
Today |
DateOnly |
Current local date |
SystemClock
Real implementation of IClock that delegates to DateTimeOffset.Now, DateTimeOffset.UtcNow, and DateTime.Today.
FakeClock
| Member | Description |
|---|---|
FakeClock(DateTimeOffset initialTime) |
Creates a fake clock at the given time |
Now |
Returns the current fake local time |
UtcNow |
Returns the current fake UTC time |
Today |
Returns the current fake date |
Advance(TimeSpan duration) |
Moves time forward by the specified duration |
SetTime(DateTimeOffset time) |
Sets the clock to an exact time |
AutoAdvance |
If set, time advances by this amount on each property access |
DI Extensions
| Method | Description |
|---|---|
AddClock() |
Registers SystemClock as IClock singleton |
AddFakeClock(DateTimeOffset? initialTime) |
Registers FakeClock as IClock singleton |
Development
dotnet build src/Philiprehberger.Clock.csproj --configuration Release
Support
If you find this project useful:
License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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 was computed. 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.
-
net8.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.