Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class DateProvider
- {
- private static DateTime? _frozenTime;
- public static DateTime Now
- {
- get { return _frozenTime ?? DateTime.Now; }
- }
- public static DateTime NowTruncated
- {
- get
- {
- return _frozenTime.HasValue
- ? new DateTime(_frozenTime.Value.Year, frozenTime.Value.Month, frozenTime.Value.Day,
- frozenTime.Value.Hour, frozenTime.Value.Minute, _frozenTime.Value.Second)
- : DateTime.Now;
- }
- }
- public static DateTime Today
- {
- get { return Now.Date; }
- }
- public static IDisposable FreezeTime(DateTime theFrozenTime)
- {
- _frozenTime = theFrozenTime;
- return new DisposableAction(() => _frozenTime = null);
- }
- public static bool IsFrozen()
- {
- return _frozenTime.HasValue;
- }
- public static void UnFreezeTime()
- {
- _frozenTime = null;
- }
- }
- /// <summary>
- /// Better sytnax for context operation.
- /// Wraps a delegate that is executed when the Dispose method is called.
- /// This allows to do context sensitive things easily.
- /// Basically, it mimics Java's anonymous classes.
- /// </summary>
- public class DisposableAction : IDisposable
- {
- readonly Proc action;
- /// <summary>
- /// Initializes a new instance of the <see cref="DisposableAction"/> class.
- /// </summary>
- /// <param name="action">The action to execute on dispose</param>
- public DisposableAction(Proc action)
- {
- if (action == null)
- throw new ArgumentNullException("action");
- this.action = action;
- }
- /// <summary>
- /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
- /// </summary>
- public virtual void Dispose()
- {
- action();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement