Provides an abstraction of ISystemClock
to assist application testing along with implementations that support 1s and 1ms accuracy.
Depending on the underlying Operating System, the accuracy of DateTime.UtcNow
on .NET Framework is around ~16ms due to the usage of the RTC. Some newer .NET
Frameworks use higher resolution timers such as QPC to obtain the highest level
of accuracy. This library provides implementations of ISystemClock
with consistent
1s and 1ms accuracy.
/// <summary>
/// Abstracts the system clock to facilitate testing.
/// </summary>
public interface ISystemClock
{
/// <summary>
/// Retrieves the current system time in UTC.
/// </summary>
DateTimeOffset UtcNow { get; }
}
/// <summary>
/// Provides access to the normal system clock with accuracy in milliseconds.
/// </summary>
public interface ISystemClockMillisecondsAccuracy : ISystemClock
{
// nothing
}
public class SystemClockMillisecondsAccuracy : ISystemClockMillisecondsAccuracy { /* ... */ }
/// <summary>
/// Provides access to the normal system clock with accuracy in seconds.
/// </summary>
/// <remarks>
/// This implementation is particularly useful in JWT authentication because
/// <code>expires_in</code> only supports whole seconds and milliseconds do
/// not round-trip when serializing.
/// </remarks>
public interface ISystemClockSecondsAccuracy : ISystemClock
{
// nothing
}
public class SystemClockSecondsAccuracy : ISystemClockSecondsAccuracy { /* ... */ }
PM> Install-Package NCode.SystemClock
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<ISystemClockSecondsAccuracy, SystemClockSecondsAccuracy>();
services.AddTransient<ISystemClockMillisecondsAccuracy, SystemClockMillisecondsAccuracy>();
// or register ISystemClock with an explicit implementation
services.AddTransient<ISystemClock, SystemClockMillisecondsAccuracy>();
}
- v1.0.0 - Initial release
- v1.0.1 - Refresh build