Skip to content

Instantly share code, notes, and snippets.

@jakesays-old
Created August 27, 2014 14:50
Show Gist options
  • Save jakesays-old/ad521c9af8650f3b39d0 to your computer and use it in GitHub Desktop.
Save jakesays-old/ad521c9af8650f3b39d0 to your computer and use it in GitHub Desktop.
DateTime abstraction
using System;
using System.Diagnostics;
using Apex.Utility.Internal;
namespace Utility
{
/// <summary>
/// The SystemTime class provides a simple abstraction over DateTime.Now.
/// It uses a time provider to generate the current time. The default
/// provider just fowards to DateTime.Now/UtcNow.
/// </summary>
public static class SystemTime
{
public static DateTime Now
{
[DebuggerStepThrough]
get { return TimeProvider.CurrentProvider.Now; }
}
public static DateTime UtcNow
{
[DebuggerStepThrough]
get { return TimeProvider.CurrentProvider.UtcNow; }
}
}
}
using System;
namespace Utility.Internal
{
public abstract class TimeProvider
{
public static TimeProvider CurrentProvider = new DefaultTimeProvider();
public abstract DateTime Now { get; }
public abstract DateTime UtcNow { get; }
}
}
using System;
using System.Diagnostics;
namespace Utility.Internal
{
public class DefaultTimeProvider : TimeProvider
{
public override DateTime Now
{
[DebuggerStepThrough]
get { return DateTime.Now; }
}
public override DateTime UtcNow
{
[DebuggerStepThrough]
get { return DateTime.UtcNow; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment