A pausable clock for C#
DateTimeDebug is useful for unit-testing code that uses DateTime.Now.
The only changes required in the tested code are substitutions of DateTime.Now
with DateTimeDebug.Now.
Unit tests that use DateTimeDebug.Now are fast
because they simulate the passage of time without actually waiting,
and they are easier to write because they aren't subject to variations
in elapsed time caused by outside influences.
DateTimeDebug can also make interactive debugging easier.
This use case requires the use of debugger hooks to cause DateTimeDebug to be paused
while the program being debugged is paused.
Apparently there are no such debugger hooks for C# debugging as of this writing,
but if you're using C# to build a simulation with its own debugger,
your simulation can provide and use its own hooks.
DateTimeDebug is not a data type; all public members are static.
Nowis aDateTimevalue, initially identical to the value ofDateTime.Now, possibly paused, possibly lagging behindDateTime.Now.- The behavior of
Nowcan be influenced by the other properties. - Changing
NowaffectsLag.
Runningis the running/paused state ofNow.Runningistrueby default.- Changing
RunningtofalsecausesNowto pause and make no further progress. - Changing
RunningtotruecausesNowto resume, lagging behindDateTime.Nowby the accumulatedLagcaused by pausing.
Lagis the amount of time thatNowlags behindDateTime.Now.Lagis0by default.- The behavior of
Lagcan be influenced by the other properties. - Changing
LagaffectsNow.
The code:
- DateTimeDebug.cs
- Demo.cs (called by Test.cs and Program.cs)