Skip to content
Merged
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
Cleanup GetLocalUtcOffset
  • Loading branch information
mdh1418 committed Aug 23, 2022
commit 96bf37faeed907999679d9bad2f0a4cfe4a30efc
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ private static TimeZoneInfo GetLocalTimeZoneCore()

private static TimeSpan? _localUtcOffset;
private static object _localUtcOffsetLock = new();
private static Thread? _loadAndroidTZData;
// Shortcut for TimeZoneInfo.Local.GetUtcOffset
internal static TimeSpan GetLocalUtcOffset(DateTime dateTime, TimeZoneInfoOptions flags)
{
Expand All @@ -153,19 +154,24 @@ internal static TimeSpan GetLocalUtcOffset(DateTime dateTime, TimeZoneInfoOption
return cachedData.Local.GetUtcOffset(dateTime, flags, cachedData);
}

if (_localUtcOffset == null)
if (_localUtcOffset == null && _loadAndroidTZData == null)
{
lock (_localUtcOffsetLock)
{
if (_localUtcOffset == null)
if (_localUtcOffset != null)
{
Thread loadAndroidTZData = new Thread(() => {
CachedData cachedData = s_cachedData;
return cachedData.Local.GetUtcOffset(dateTime, flags, cachedData);
}
if (_loadAndroidTZData == null)
{
_loadAndroidTZData = new Thread(() => {
CachedData cachedData = s_cachedData;
_localUtcOffset = cachedData.Local.GetUtcOffset(dateTime, flags, cachedData);
Thread.Sleep(1000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like @eerhardt mentioned the 1 second here, a slow app could be more than 1 second, is there a way this can use ManualResetEvent? (or whatever the "good" reset event is)

Then something in xamarin/xamarin-android, maybe Android.App.Activity.OnCreate() could call into the runtime. @grendello do you have any thoughts how we could signal to the runtime the app is done loading?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the app takes more than 1s to start, then the time to load TZ doesn't really matter. There's no reason to complicate the code with signalling, especially that Android is notoriously hard in this respect... The app is done loading when the Displayed: app line is output to logcat, but there's no way to catch that event in the app itself.

1s is very generous, but we can increase it to 1.5s if we think this is better. However, anything above ~700ms is perceived as "slow" by the user, so I really wouldn't worry about this.

});
loadAndroidTZData.IsBackground = true;
loadAndroidTZData.Start();
Thread.Sleep(1000);
_loadAndroidTZData.IsBackground = true;
_loadAndroidTZData.Start();
}
}
}
Expand Down