TimeProvider
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
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)]
[ExcludeFromCodeCoverage]
internal abstract class TimeProvider
{
[global::System.Runtime.CompilerServices.NullableContext(0)]
[ExcludeFromCodeCoverage]
private sealed class SystemTimeProviderTimer : ITimer, IDisposable, IAsyncDisposable
{
[global::System.Runtime.CompilerServices.NullableContext(2)]
[global::System.Runtime.CompilerServices.Nullable(0)]
private sealed class TimerState
{
[global::System.Runtime.CompilerServices.Nullable(1)]
[field: global::System.Runtime.CompilerServices.Nullable(1)]
public TimerCallback Callback {
[global::System.Runtime.CompilerServices.NullableContext(1)]
get;
}
public object State { get; }
public Timer Timer { get; set; }
[global::System.Runtime.CompilerServices.NullableContext(1)]
public TimerState(TimerCallback callback, [global::System.Runtime.CompilerServices.Nullable(2)] object state)
{
Callback = callback;
State = state;
}
}
[global::System.Runtime.CompilerServices.Nullable(1)]
private readonly Timer _timer;
[global::System.Runtime.CompilerServices.NullableContext(1)]
public SystemTimeProviderTimer(TimeSpan dueTime, TimeSpan period, TimerCallback callback, [global::System.Runtime.CompilerServices.Nullable(2)] 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");
if (num > 4294967294)
throw new ArgumentOutOfRangeException("dueTime");
if (num2 < -1)
throw new ArgumentOutOfRangeException("periodTm");
if (num2 > 4294967294)
throw new ArgumentOutOfRangeException("periodTm");
return ((uint)num, (uint)num2);
}
}
[global::System.Runtime.CompilerServices.NullableContext(0)]
[ExcludeFromCodeCoverage]
private sealed class SystemTimeProvider : TimeProvider
{
internal SystemTimeProvider()
{
}
}
private static readonly long MinDateTicks = DateTime.MinValue.Ticks;
private static readonly long MaxDateTicks = DateTime.MaxValue.Ticks;
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();
TimeSpan utcOffset = localTimeZone.GetUtcOffset(utcNow);
long num = utcNow.Ticks + utcOffset.Ticks;
if ((ulong)num > (ulong)MaxDateTicks)
num = ((num < MinDateTicks) ? MinDateTicks : 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();
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);
}
}
}