Skip to content

Instantly share code, notes, and snippets.

@tomaustin700
Created March 20, 2018 10:55
Show Gist options
  • Save tomaustin700/339d18b9802e9f757ae74ce30c5a49b7 to your computer and use it in GitHub Desktop.
Save tomaustin700/339d18b9802e9f757ae74ce30c5a49b7 to your computer and use it in GitHub Desktop.
/// <summary>
/// Used for getting DateTime.Now(), time is changeable for unit testing
/// </summary>
public static class SystemTime
{
/// <summary> Normally this is a pass-through to DateTime.Now, but it can be overridden with SetDateTime( .. ) for testing or debugging.
/// </summary>
public static Func<DateTime> Now = () => DateTime.Now;
/// <summary> Set time to return when SystemTime.Now() is called.
/// </summary>
public static void SetDateTime(DateTime dateTimeNow)
{
Now = () => dateTimeNow;
}
/// <summary> Resets SystemTime.Now() to return DateTime.Now.
/// </summary>
public static void ResetDateTime()
{
Now = () => DateTime.Now;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment