<PackageReference Include="Microsoft.Bcl.TimeProvider" Version="8.0.0-preview.5.23280.8" />

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) { (uint duration, uint periodTime) valueTuple = CheckAndGetValues(dueTime, period); uint item = valueTuple.duration; uint item2 = valueTuple.periodTime; TimerState timerState = new TimerState(callback, state); timerState.Timer = (_timer = new Timer(delegate(object s) { TimerState timerState2 = (TimerState)s; timerState2.Callback(timerState2.State); }, timerState, item, item2)); } public bool Change(TimeSpan dueTime, TimeSpan period) { (uint duration, uint periodTime) valueTuple = CheckAndGetValues(dueTime, period); uint item = valueTuple.duration; uint item2 = valueTuple.periodTime; try { return _timer.Change(item, item2); } catch (ObjectDisposedException) { return false; } } public void Dispose() { _timer.Dispose(); } public ValueTask DisposeAsync() { _timer.Dispose(); return default(ValueTask); } private static (uint duration, uint periodTime) CheckAndGetValues(TimeSpan dueTime, TimeSpan periodTime) { long num = (long)dueTime.TotalMilliseconds; long num2 = (long)periodTime.TotalMilliseconds; if (num < -1) throw new ArgumentOutOfRangeException("dueTime", num, global::System.SR.Format(global::System.SR.ArgumentOutOfRange_Generic_MustBeGreaterOrEqual, "dueTime", -1)); if (num > 4294967294) throw new ArgumentOutOfRangeException("dueTime", num, global::System.SR.Format(global::System.SR.ArgumentOutOfRange_Generic_MustBeLessOrEqual, "dueTime", 4294967294)); if (num2 < -1) throw new ArgumentOutOfRangeException("periodTm", num2, global::System.SR.Format(global::System.SR.ArgumentOutOfRange_Generic_MustBeGreaterOrEqual, "periodTm", -1)); if (num2 > 4294967294) throw new ArgumentOutOfRangeException("periodTm", num2, global::System.SR.Format(global::System.SR.ArgumentOutOfRange_Generic_MustBeLessOrEqual, "periodTm", 4294967294)); return ((uint)num, (uint)num2); } } 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); 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; } } }