<PackageReference Include="Polly.Core" Version="8.0.0-alpha.1" />

TimeProvider

abstract class TimeProvider
TEMPORARY ONLY, to be replaced with System.TimeProvider - https://github.com/dotnet/runtime/issues/36617 later.
using System; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; namespace Polly.Utils { [global::System.Runtime.CompilerServices.NullableContext(1)] [global::System.Runtime.CompilerServices.Nullable(0)] internal abstract class TimeProvider { [global::System.Runtime.CompilerServices.Nullable(0)] private sealed class SystemTimeProvider : TimeProvider { public override DateTimeOffset UtcNow => DateTimeOffset.UtcNow; public SystemTimeProvider() : base(Stopwatch.Frequency) { } public override long GetTimestamp() { return Stopwatch.GetTimestamp(); } public override Task Delay(TimeSpan delay, CancellationToken cancellationToken = default(CancellationToken)) { return Task.Delay(delay, cancellationToken); } public override void CancelAfter(CancellationTokenSource source, TimeSpan delay) { source.CancelAfter(delay); } } private readonly double _tickFrequency; public static TimeProvider System { get; } = new SystemTimeProvider(); public abstract DateTimeOffset UtcNow { get; } public long TimestampFrequency { get; } protected TimeProvider(long timestampFrequency) { TimestampFrequency = timestampFrequency; _tickFrequency = 10000000 / (double)TimestampFrequency; } public abstract long GetTimestamp(); public TimeSpan GetElapsedTime(long startingTimestamp, long endingTimestamp) { return new TimeSpan((long)((double)(endingTimestamp - startingTimestamp) * _tickFrequency)); } public TimeSpan GetElapsedTime(long startingTimestamp) { return GetElapsedTime(startingTimestamp, GetTimestamp()); } public abstract Task Delay(TimeSpan delay, CancellationToken cancellationToken = default(CancellationToken)); public abstract void CancelAfter(CancellationTokenSource source, TimeSpan delay); } }