namespace StellaOps.TestKit.Time;
///
/// Deterministic clock for testing that returns a fixed time.
///
public sealed class DeterministicClock
{
private DateTimeOffset _currentTime;
///
/// Creates a new deterministic clock with the specified initial time.
///
/// The initial time. If null, uses 2025-01-01T00:00:00Z.
public DeterministicClock(DateTimeOffset? initialTime = null)
{
_currentTime = initialTime ?? new DateTimeOffset(2025, 1, 1, 0, 0, 0, TimeSpan.Zero);
}
///
/// Gets the current time.
///
public DateTimeOffset UtcNow => _currentTime;
///
/// Advances the clock by the specified duration.
///
/// The duration to advance.
public void Advance(TimeSpan duration)
{
_currentTime = _currentTime.Add(duration);
}
///
/// Sets the clock to a specific time.
///
/// The time to set.
public void SetTime(DateTimeOffset time)
{
_currentTime = time;
}
///
/// Resets the clock to the initial time.
///
public void Reset()
{
_currentTime = new DateTimeOffset(2025, 1, 1, 0, 0, 0, TimeSpan.Zero);
}
}
///
/// Extensions for working with deterministic clocks in tests.
///
public static class DeterministicClockExtensions
{
///
/// Standard test epoch: 2025-01-01T00:00:00Z
///
public static readonly DateTimeOffset TestEpoch = new(2025, 1, 1, 0, 0, 0, TimeSpan.Zero);
///
/// Creates a clock at the standard test epoch.
///
public static DeterministicClock AtTestEpoch() => new(TestEpoch);
///
/// Creates a clock at a specific ISO 8601 timestamp.
///
public static DeterministicClock At(string iso8601) => new(DateTimeOffset.Parse(iso8601));
}