WALTER | Workable Algorithms for Location-aware Transmission, Encryption Response

IClock Interface

Defines a mechanism for retrieving the current date and time, allowing for consistent time handling within .NET applications. Ideal for scenarios requiring time manipulation, such as unit testing, stock exchange replay, or cyber attack detection analysis.

Namespace:  Walter
Assembly:  Walter (in Walter.dll)

Syntax


public interface IClock

Remarks


Implement the IClock interface to enable dependency injection for time-related functions. This facilitates the easy substitution of the clock implementation with a mock or test implementation, like TestClock, during testing. The static Instance property is used for accessing the current time and can be overridden in unit tests or integration tests to simulate different time scenarios.

Examples


This example demonstrates using IClock in a class and in unit tests for time-sensitive operations:
Using IClock in a class and unit test
// Class using the IClock
class Record
{
    public Record(string name)
    {
        Time = IClock.Instance.Now; // Set the current time
        Name = name;
    }

    /// <summary>
    /// Time the action was recorded
    /// </summary>
    public DateTime Time { get; }

    // Other properties...
}

// In your unit test or replay class, validate logic based on date and time
var clock = new Walter.TestClock(TimeSeries[0].Time); // Initialize TestClock with a specific time
Walter.IClock.Instance = clock; // Swap the IClock instance with TestClock

foreach (var item in TimeSeries)
{
    var laps = item.Time - IClock.Instance.Now; // Calculate time difference
    clock.Add(laps); // Move the clock forward
    var record = new Record(item.Activity);
    // Perform time-sensitive activity using the time set by IClock
    bool isAllowed = UserCanLogin(record);
}