Created
August 6, 2023 12:02
-
-
Save nishanc/dcbe44e0306cf8373d02776331bf66f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get system time. | |
DateTimeOffset utcNow = TimeProvider.System.GetUtcNow(); | |
DateTimeOffset localNow = TimeProvider.System.GetLocalNow(); | |
// Create a time provider that works with a | |
// time zone that's different than the local time zone. | |
private class ZonedTimeProvider : TimeProvider | |
{ | |
private TimeZoneInfo _zoneInfo; | |
public ZonedTimeProvider(TimeZoneInfo zoneInfo) : base() | |
{ | |
_zoneInfo = zoneInfo ?? TimeZoneInfo.Local; | |
} | |
public override TimeZoneInfo LocalTimeZone => _zoneInfo; | |
public static TimeProvider FromLocalTimeZone(TimeZoneInfo zoneInfo) => | |
new ZonedTimeProvider(zoneInfo); | |
} | |
// Create a timer using a time provider. | |
ITimer timer = timeProvider.CreateTimer(callBack, state, delay, Timeout.InfiniteTimeSpan); | |
// Measure a period using the system time provider. | |
long providerTimestamp1 = TimeProvider.System.GetTimestamp(); | |
long providerTimestamp2 = TimeProvider.System.GetTimestamp(); | |
var period = GetElapsedTime(providerTimestamp1, providerTimestamp2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment