Current time abstraction coming to .NET
It is often necessary in computer systems to get the current date and time. .NET has had a straightforward way to do that since its inception. What it hasn’t had is a built-in way to customize the behavior, particularly for mocking during automated tests.
Quite why this situation has persisted for so long is a bit of mystery. The importance of automated tests has only risen over time and questions about how to deal with this crop up quite frequently. The general solution is to provide an abstraction that you can inject and mock easily.
The popular date and time library Noda Time includes IClock
, and any developers are often directed to use Noda Time for anything making use of anything but the simplest of time-based logic. But it is still odd there is nothing built-in, especially considering there is an existing private implementation in .NET, as well as several in ASP.NET.
Well .NET 8 will finally be getting System.TimeProvider
:
namespace System
{
/// <summary>Provides an abstraction for time. </summary>
public abstract class TimeProvider
{
protected TimeProvider();
public static TimeProvider System { get; }
public static TimeProvider FromLocalTimeZone(TimeZoneInfo timeZone);
public abstract DateTimeOffset UtcNow { get; }
public DateTimeOffset LocalNow { get; }
public abstract TimeZoneInfo LocalTimeZone { get; }
public abstract long GetTimestamp();
public abstract long TimestampFrequency { get; }
public TimeSpan GetElapsedTime(long startingTimestamp, long endingTimestamp);
public abstract ITimer CreateTimer(TimerCallback callback, object? state, TimeSpan dueTime, TimeSpan period);
}
}
Here is the original GitHub issue and the pull request implementing it.
As well as providing a mockable abstraction to get the current time (including a default implementation providing the system time), it also adds supports to a bunch of other APIs to use a specified instance instead including Task
and Timer
.
And, best of all, despite my earlier claim that this is a .NET 8 feature, this will also be made available as a package targeting .NET Standard 2.0, so it can be used on .NET Framework and other targets.