Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address feedback
  • Loading branch information
mdh1418 authored and github-actions committed Sep 1, 2022
commit 8f4abd0591d14f4fcadaf392e74e7dc2a3c8231f
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public readonly partial struct DateTimeOffset
private static readonly object s_localUtcOffsetLock = new();
private static Thread? s_loadAndroidTZData;

// TryGetNow is a helper function on Android responsible for:
// TryGetNowOffset is a helper function on Android responsible for:
// 1) quickly returning a fast path offset when first called
// 2) starting a background thread to pursue the original implementation
//
Expand All @@ -23,7 +23,7 @@ public readonly partial struct DateTimeOffset
// So, on first call, we return the fast path and start a background thread to pursue the original
// implementation which eventually loads AndroidTZData.

private static TimeSpan TryGetNow()
private static TimeSpan TryGetNowOffset()
{
if (s_androidTZDataLoaded) // The background thread finished, the cache is loaded.
return TimeZoneInfo.GetLocalUtcOffset(DateTime.UtcNow, TimeZoneInfoOptions.NoThrowOnInvalidTime);
Expand All @@ -40,7 +40,7 @@ private static TimeSpan TryGetNow()
if (s_loadAndroidTZData == null)
{
s_loadAndroidTZData = new Thread(() => {
Thread.Sleep(1000);
Thread.Sleep(1000); // Delay the background thread to avoid impacting startup, if it still coincides, startup is already perceived as slow
_ = TimeZoneInfo.GetLocalUtcOffset(DateTime.UtcNow, TimeZoneInfoOptions.NoThrowOnInvalidTime);
lock (s_localUtcOffsetLock)
{
Expand All @@ -55,8 +55,8 @@ private static TimeSpan TryGetNow()
}

object? localDateTimeOffset = AppContext.GetData("System.TimeZoneInfo.LocalDateTimeOffset");
if (localDateTimeOffset == null)
return TimeZoneInfo.GetLocalUtcOffset(DateTime.UtcNow, TimeZoneInfoOptions.NoThrowOnInvalidTime); // If no offset property provided through monovm app context, default
if (localDateTimeOffset == null) // If no offset property provided through monovm app context, default
return TimeZoneInfo.GetLocalUtcOffset(DateTime.UtcNow, TimeZoneInfoOptions.NoThrowOnInvalidTime);

int localDateTimeOffsetSeconds = Convert.ToInt32(localDateTimeOffset);
TimeSpan offset = TimeSpan.FromSeconds(localDateTimeOffsetSeconds);
Expand All @@ -68,7 +68,7 @@ public static DateTimeOffset Now
get
{
DateTime utcDateTime = DateTime.UtcNow;
TimeSpan offset = TryGetNow();
TimeSpan offset = TryGetNowOffset();
long localTicks = utcDateTime.Ticks + offset.Ticks;
if (localTicks < DateTime.MinTicks || localTicks > DateTime.MaxTicks)
throw new ArgumentException(SR.Arg_ArgumentOutOfRangeException);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

namespace System
{
public readonly partial struct DateTimeOffset
{
// Returns a DateTimeOffset representing the current date and time. The
// resolution of the returned value depends on the system timer.
public static DateTimeOffset Now => ToLocalTime(DateTime.UtcNow, true);
}
public readonly partial struct DateTimeOffset
{
// Returns a DateTimeOffset representing the current date and time. The
// resolution of the returned value depends on the system timer.
public static DateTimeOffset Now => ToLocalTime(DateTime.UtcNow, true);
}
}