mattjohnsonpint / TimeZoneNames

Provides a complete source of localized time zone names and abbreviations.
MIT License
196 stars 32 forks source link

Can't get time zones for country in Xamarin Forms App #95

Closed ArthurLlew closed 1 year ago

ArthurLlew commented 1 year ago

Reproduction steps: put var zones = TZNames.GetTimeZonesForCountry("AU", "en"); in any place of Xamarin Forms App 1 2

mattjohnsonpint commented 1 year ago

Hi again! 👋 😃

As I showed on #94, it's working for me. Can you provide any additional information? The full stack trace would be very helpful.

Thanks.

ArthurLlew commented 1 year ago

Yes, what a coincidence 👋 😃 Well, if I use library stack trace ends at this call. If I'm using your source code directly, stack trace is 1 2 3 4 5

ArthurLlew commented 1 year ago

The project, that I'm currently in, has a lot of "update holes". Since this is more likely System problem and you don't have one (and you are for sure using newly created project), I just need to update some settings. What is .NET version, targeted platform, .NET Framework and ext. for you?

mattjohnsonpint commented 1 year ago

The last screenshot doesn't make much sense - KnownWIndowsTimeZoneIds.Contains would never throw a TimeZoneNotFoundException. It's just a simple dictionary lookup.

I think this is one of those cases where the debug line numbers are off by one. The actual exception must be being thrown by the WindowsToIana call below it. Still, you wouldn't have gotten there unless somehow KnownWindowsTimeZoneIds actually contained IANA time zone IDs ("Australia/Perth").

Either way, I think #96 will resolve the problem. This is an optimization over your proposed PR in that it attempts to performs the conversion without raising an exception, and it only does so in the code paths where a Windows ID might be a valid input.

ArthurLlew commented 1 year ago

I decided to create my own blank Xamarin Forms project and it all works fine. Seems like some components in my project are tweaked in a wrong way. I'm closing issue. Thanks for support!

ArthurLlew commented 1 year ago

I found out recently, that I saw that exception because in Debugger in exception settings System.TimeZoneNotFoundException was checked on. When I checked that option on in blank project, I saw that exception. And, interesting enough, this exception does not crash Application for some reason.

mattjohnsonpint commented 1 year ago

Ahh.. That explains it. You were only running into this in Visual Studio while catching that as a "first-chance" exception. It happened on the call to TZConvert.KnownWindowsTimeZoneIds, because that was when the TZConvert static constructor was invoked. During that initialization, this code is called:

try
{
    var tzi = TimeZoneInfo.FindSystemTimeZoneById(name);
    zones.Add(tzi.Id, tzi);
}
catch
{
    // ignored
}

This is by design. Normally, any exception raised by FindSystemTimeZoneById would be swallowed by the catch clause. But in your case, you were asking VS to stop there to show you the exception anyway.

mattjohnsonpint commented 1 year ago

Your earlier screenshot showed this as well:

image

Normally, only the second of those two checkboxes would be checked.