A lightweight implementation of SystemClock that will help make your applications more testable when dealing with dates and times.
C#
Latest commit 3b29d00 Apr 23, 2014 @ChrisMissal ChrisMissal version bump
increase minor version due to added functionality in a backwards-compatible manner
Permalink
Failed to load latest commit information.
.gitignore initial code May 10, 2013
LICENSE.txt Add title to license May 10, 2013
README.md fixed up readme docs May 15, 2013
SystemClock.cs
SystemClock.nuspec version bump Apr 22, 2014

README.md

HeadspringTime

A lightweight implementation of SystemClock that will help make your applications more testable when dealing with dates and times.

General Usage

You can use SystemClock just like you would with System.DateTime:

var now = SystemClock.UtcNow; // instead of DateTime.UtcNow

So far, no visual benefit, but if you have code that assigns the current time, you can temporarily override this value by using the Stub method (which is IDisposable and can take advantage of the C# using keyword.

using (SystemClock.Stub(new DateTime(2001, 5, 5))
{
    var now = SystemClock.UtcNow; // will be May 5th, 2001
}

This is very helpful when testing business logic that relies on dates and times.

Notes

You may notice that SystemClock uses UtcNow instead of Now. This is because using UTC dates and times throughout an application makes it much easier to deal with time zones and local time.

For more information on how to convert to a different time zone: TimeZoneInfo.ConvertTimeFromUtc or to local time using DateTime.ToLocalTime