<PackageReference Include="Microsoft.Bcl.TimeProvider" Version="9.0.0-preview.2.24128.5" />

TimeProvider

public abstract class TimeProvider
Provides an abstraction for time.
using System.Diagnostics; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; namespace System { [global::System.Runtime.CompilerServices.NullableContext(1)] [global::System.Runtime.CompilerServices.Nullable(0)] public abstract class TimeProvider { private sealed class SystemTimeProviderTimer : ITimer, IDisposable, IAsyncDisposable { private sealed class TimerState { public TimerCallback Callback { get; } = callback; public object State { get; } = state; public Timer Timer { get; set; } public TimerState(TimerCallback callback, object state) { } } private readonly Timer _timer; public SystemTimeProviderTimer(TimeSpan dueTime, TimeSpan period, TimerCallback callback, object state) { TimerState timerState = new TimerState(callback, state); timerState.Timer = (_timer = new Timer(delegate(object s) { TimerState timerState2 = (TimerState)s; timerState2.Callback(timerState2.State); }, timerState, dueTime, period)); } public bool Change(TimeSpan dueTime, TimeSpan period) { try { return _timer.Change(dueTime, period); } catch (ObjectDisposedException) { return false; } } public void Dispose() { _timer.Dispose(); } public ValueTask DisposeAsync() { _timer.Dispose(); return default(ValueTask); } } private sealed class SystemTimeProvider : TimeProvider { internal SystemTimeProvider() { } } private static readonly long s_minDateTicks; private static readonly long s_maxDateTicks; public static TimeProvider System { get; } = new SystemTimeProvider(); public virtual TimeZoneInfo LocalTimeZone => TimeZoneInfo.Local; public virtual long TimestampFrequency => Stopwatch.Frequency; public virtual DateTimeOffset GetUtcNow() { return DateTimeOffset.UtcNow; } public DateTimeOffset GetLocalNow() { DateTimeOffset utcNow = GetUtcNow(); TimeZoneInfo localTimeZone = LocalTimeZone; if (localTimeZone == null) throw new InvalidOperationException(global::System.SR.InvalidOperation_TimeProviderNullLocalTimeZone); TimeSpan utcOffset = localTimeZone.GetUtcOffset(utcNow); if (utcOffset.Ticks == 0) return utcNow; long num = utcNow.Ticks + utcOffset.Ticks; if ((ulong)num > (ulong)s_maxDateTicks) num = ((num < s_minDateTicks) ? s_minDateTicks : s_maxDateTicks); return new DateTimeOffset(num, utcOffset); } public virtual long GetTimestamp() { return Stopwatch.GetTimestamp(); } public TimeSpan GetElapsedTime(long startingTimestamp, long endingTimestamp) { long timestampFrequency = TimestampFrequency; if (timestampFrequency <= 0) throw new InvalidOperationException(global::System.SR.InvalidOperation_TimeProviderInvalidTimestampFrequency); return new TimeSpan((long)((double)(endingTimestamp - startingTimestamp) * (10000000 / (double)timestampFrequency))); } public TimeSpan GetElapsedTime(long startingTimestamp) { return GetElapsedTime(startingTimestamp, GetTimestamp()); } public virtual ITimer CreateTimer(TimerCallback callback, [global::System.Runtime.CompilerServices.Nullable(2)] object state, TimeSpan dueTime, TimeSpan period) { if (callback == null) throw new ArgumentNullException("callback"); return new SystemTimeProviderTimer(dueTime, period, callback, state); } static TimeProvider() { DateTime dateTime = DateTime.MinValue; s_minDateTicks = dateTime.Ticks; dateTime = DateTime.MaxValue; s_maxDateTicks = dateTime.Ticks; } } }