Created
June 14, 2011 16:30
-
-
Save joshuaflanagan/1025274 to your computer and use it in GitHub Desktop.
Stubbable replacement for DateTime.Now
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class SystemClock | |
{ | |
static SystemClock() | |
{ | |
Live(); | |
} | |
public static DateTime Now | |
{ | |
get { return _getTime(); } | |
} | |
public static void Live() | |
{ | |
_getTime = () => DateTime.UtcNow; | |
} | |
public static void Stub(DateTime theTimeToReturn) | |
{ | |
_getTime = () => theTimeToReturn; | |
} | |
public static DateTime Today { get { return _getTime().Date; } } | |
public static void Stub(Func<DateTime> getTime) | |
{ | |
_getTime = getTime; | |
} | |
private static Func<DateTime> _getTime; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment