Skip to content

Instantly share code, notes, and snippets.

@ssajous
Last active January 7, 2016 15:58
Show Gist options
  • Save ssajous/e1e4e794e88629165314 to your computer and use it in GitHub Desktop.
Save ssajous/e1e4e794e88629165314 to your computer and use it in GitHub Desktop.
Time Provider Ambient Context for mockable date times
public abstract class TimeProvider
{
private static TimeProvider current;
static TimeProvider()
{
TimeProvider.current =
new DefaultTimeProvider();
}
public static TimeProvider Current
{
get { return TimeProvider.current; }
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
TimeProvider.current = value;
}
}
public abstract DateTime UtcNow { get; }
public static void ResetToDefault()
{
TimeProvider.current =
new DefaultTimeProvider();
}
}
public class DefaultTimeProvider : TimeProvider
{
public override DateTime UtcNow
{
get { return DateTime.UtcNow; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment