Skip to content

About

A simple abstraction over C#'s DateTime.Now, DateTime.Today and DateTime.UtcNow so you can control these values in your tests.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

9 stars

Watchers

1 watching

Forks

Repository files navigation

SimpleDateTimeProvider. Your tests decide what time it is.

Build Pipeline NuGet NuGet Downloads License

A tiny abstraction over DateTime.Now, DateTime.Today and DateTime.UtcNow, so your tests decide what time it is instead of the clock on the build server.

No more tests that only fail at midnight, on the 29th of February, or in a time zone nobody on the team lives in.

Install

dotnet add package SimpleDateTimeProvider

It has no dependencies, so that's the whole shopping list.

Use it

Ask for IDateTimeProvider wherever you'd have reached for DateTime. Here's a free trial that needs to know when it's over:

using System;
using SimpleDateTimeProvider;

public class TrialChecker
{
    private readonly IDateTimeProvider dateTimeProvider;

    public TrialChecker(IDateTimeProvider dateTimeProvider)
    {
        this.dateTimeProvider = dateTimeProvider;
    }

    public bool HasEnded(DateTime endsUtc)
    {
        return this.dateTimeProvider.UtcNow >= endsUtc;
    }
}

With dependency injection, register the real clock against the interface:

_ = services.AddSingleton<IDateTimeProvider, SystemDateTimeProvider>();
_ = services.AddSingleton<TrialChecker>();

No container? A console app or an older .NET Framework project can just new one up:

var checker = new TrialChecker(new SystemDateTimeProvider());

SystemDateTimeProvider hands straight through to DateTime and keeps no state, so one for the whole app is plenty. In production nothing changes.

Test it

In your tests, swap in MockDateTimeProvider and set whichever values the test needs. They're settable at any point, so one test can stand just before the trial ends, then move the clock on and check again:

using System;
using Shouldly;
using SimpleDateTimeProvider;
using Xunit;

public class TrialCheckerTests
{
    [Fact]
    public void HasEnded_ShouldFlip_WhenTheTrialEnds()
    {
        // Arrange
        var endsUtc = new DateTime(2024, 2, 29, 0, 0, 0, DateTimeKind.Utc);
        var dateTimeProvider = new MockDateTimeProvider { UtcNow = endsUtc.AddSeconds(-1) };
        var checker = new TrialChecker(dateTimeProvider);

        // Act and assert: one second to go
        checker.HasEnded(endsUtc).ShouldBeFalse();

        // Move the clock on
        dateTimeProvider.UtcNow = endsUtc;

        // Act and assert: time's up
        checker.HasEnded(endsUtc).ShouldBeTrue();
    }
}

No waiting, no Thread.Sleep, and it passes on the 29th of February like any other day. Give each test its own MockDateTimeProvider, so tests running in parallel can't move each other's clocks. The example uses xUnit and Shouldly, but any test framework will do.

Forget to set a value and you get a MockDateTimeNotSetException telling you which one, rather than a quiet 01/01/0001 that sends you on a long walk through the debugger.

The sample folder has a console app and its tests with all three values.

What about TimeProvider?

Fair question. .NET 8 brought TimeProvider, with FakeTimeProvider in Microsoft.Extensions.TimeProvider.Testing for tests, and Microsoft.Bcl.TimeProvider takes it back to older frameworks. It also does timers and time zones. If you're starting fresh on modern .NET, it's a fine choice, and I've written about bending DateTime in .NET 8 with it.

SimpleDateTimeProvider is for when you'd rather have three properties you can just set, or your code already uses it and has better things to do than migrate. They're friends, not rivals.

Supported frameworks

The package ships a .NET Standard 2.0 build and a .NET 10 build. .NET Standard 2.0 covers:

  • .NET Framework 4.6.1 and later (4.7.2 and later is happier)
  • .NET Core 2.0 and later, and every .NET since
  • Mono, Xamarin and Unity

Every change is tested on .NET 10, and on .NET Framework 4.8 on Windows, so the old frameworks aren't just a promise on a badge.

Releases

It looks after itself. Dependabot opens one pull request on the 15th of each month, and on the 1st anything merged that changes the package goes to NuGet with a GitHub release to match. Versions are the build date plus a build number, so 2026.12.02.105 was built on 2 December 2026. NuGet drops the leading zero and lists it as 2026.12.2.105.

Contributing

Bug reports, ideas and pull requests are all welcome. CONTRIBUTING.md has how to build and test it, and the code of conduct has how to be excellent to each other. Security problems go through the security policy, not an issue.

Thanks

A few people showed me why mocking DateTime is worth the trouble. Thanks to Andrew Harcourt for pushing it at unnamed previous employers, and to Andrew Wickens and Jared Wilton, my team at Tigerspike, for keeping me honest and always learning.

If it saves you some time, you can sponsor the work. It also funds Lean Mean Clean Apps, and the coffee.

Licence

MIT, so do what you like with it.

About

A simple abstraction over C#'s DateTime.Now, DateTime.Today and DateTime.UtcNow so you can control these values in your tests.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

9 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages