Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.TimeZoneNotFoundException: 'The time zone ID '...' was not found on the local computer.' #53

Open
mizuhs opened this issue Mar 16, 2024 · 4 comments

Comments

@mizuhs
Copy link

mizuhs commented Mar 16, 2024

Hi,
First of all, thanks for your amazing plugin! I've been running on this issue while attempting to load the events on Android. The application throws an exception and I'm not sure about how to work around this issue.

Loading/creating calendars and creating events work just fine, but loading events will throw this exception every time. I've tried to create the event with both DateTimeOffset.Now and DateTimeOffset.UtcNow to no avail. I have also tried to construct a new DateTime directly in the parameters, with Year, Month, Day, Hour, Minute, Second (with and without specifying a Kind parameter, both with Utc and Unspecified values).

Nothing has worked so far.
Interestingly, if I run the app on a Windows platform, it works just fine (Blazor MAUI App).
Plugin version is 2.0.0

@jfversluis
Copy link
Owner

Hi there, thanks for the report! I think I have seen this before... So just to be clear: this is a Blazor Hybrid app? Are you running this on an emulator or physical device? In either case, what are the details? I'd love to see a reproduction so I can see what is going on and fix it!

@mizuhs
Copy link
Author

mizuhs commented Mar 20, 2024

Hi, Mr. Versluis. I'm sorry I couldn't reach back sooner. Thanks for your reply.

Yes, this is indeed a Blazor Hybrid app and I'm running it on an actual Android device (Nokia 5.4 running on android 12). At the moment I can't really try it on an emulator for storage reasons.

As for the code, I'm simply running this bit for testing purposes:
var EventID = await CalendarStore.Default.CreateEvent(CalendarID, "Title", "Description", "Location", DateTime.Now.AddMinutes(5), DateTime.Now.AddMinutes(10));

As I mentioned, I have also tried using DateTimeOffset.Now / DateTimeOffset.UtcNow instead.

Creating an event works, but the problem happens when I try to fetch the events for this calendar or when I try to GetEvent("EventID") on the specific Ids returned from this function in my tests. I noticed that I can fetch events by calendars I have not created through the app or individually from event Ids that already exist on the phone, by guessing some Ids based on the incremental number pattern, but not the ones I create through this test.

@rojasjo
Copy link

rojasjo commented Apr 29, 2024

@jfversluis @mizuhs I encountered the same problem on Android (native app on physical device). Basically, I'm trying to fetch all events in all calendars and it happens that sometimes I get this exception (not every time):

System.TimeZoneNotFoundException: The time zone ID 'Ora standard dell’Europa centrale' was not found on the local computer.
 ---> System.InvalidTimeZoneException: The time zone ID 'Ora standard dell’Europa centrale' was found on the local computer, but the file at '/apex/com.android.tzdata/etc/tz/tzdatatzdata' was corrupt.
   --- End of inner exception stack trace ---
   at System.TimeZoneInfo.FindSystemTimeZoneById(String id)
   at System.TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTimeOffset dateTimeOffset, String destinationTimeZoneId)
   at Plugin.Maui.CalendarStore.CalendarStoreImplementation.ToEvent(ICursor cursor, List`1 projection) in /Volumes/M1/dev/opensource/Plugin.Maui.CalendarStore/src/Plugin.Maui.CalendarStore/CalendarStore.android.cs:line 541
   at Plugin.Maui.CalendarStore.CalendarStoreImplementation.ToEvents(ICursor cur, List`1 projection)+MoveNext() in /Volumes/M1/dev/opensource/Plugin.Maui.CalendarStore/src/Plugin.Maui.CalendarStore/CalendarStore.android.cs:line 529
   at System.Collections.Generic.List`1[[Plugin.Maui.CalendarStore.CalendarEvent, Plugin.Maui.CalendarStore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[CalendarEvent](IEnumerable`1 source)
   at Plugin.Maui.CalendarStore.CalendarStoreImplementation.GetEvents(String calendarId, Nullable`1 startDate, Nullable`1 endDate) in /Volumes/M1/dev/opensource/Plugin.Maui.CalendarStore/src/Plugin.Maui.CalendarStore/CalendarStore.android.cs:line 227
   at LessonsLog.Calendar.CalendarService.LoadEventsFromCalendar(String calendarId, Nullable`1 start, Nullable`1 end) in ....

The exception is thrown by the line 550 => StartDate = timezone is null ? start : TimeZoneInfo.ConvertTimeBySystemTimeZoneId(start, timezone),.

I couldn't get rid of the exception but only to fallback to TimeZoneInfo.Local or TimeZoneInfo.Utc with a similar solution:

CalendarEvent ToEvent(ICursor cursor, List<string> projection)
	{
		var timezone = cursor.GetString(projection.IndexOf(CalendarContract.Events.InterfaceConsts.EventTimezone));
		
		TimeZoneInfo? timeZoneId = null;
		if (timezone is not null)
		{
			try
			{
				timeZoneId = TimeZoneInfo.FindSystemTimeZoneById(timezone);
			}
			catch (TimeZoneNotFoundException)
			{
				timeZoneId = TimeZoneInfo.Utc;
			}
		}	
....
....
	                 StartDate = timeZoneId is null ? start : TimeZoneInfo.ConvertTime(start, timeZoneId),
			EndDate = timeZoneId is null ? end : TimeZoneInfo.ConvertTime(end, timeZoneId),
....	

@jfversluis What do you think about this? Should I create a PR to solve this issue?

@kuririn2001
Copy link

Hello all.

In my case, the program stopped at the "CreateEvent" and "ToEvent" methods in CalendarStore.android.cs.
This seems to happen because the TimeZone is not set properly.
In the case of "CreateEvent", with the current code,
"eventToInsert.Put(CalendarContract.Events.InterfaceConsts.EventTimezone, TimeZoneInfo.Local.StandardName);"
Modify this code as follows.
"eventToInsert.Put(CalendarContract.Events.InterfaceConsts.EventTimezone, TimeZoneInfo.Local.Id);"

This is because "TimeZoneInfo.Local.StandardName" does not return the TimeZone ID used by Android, unlike "TimeZoneInfo.Local.Id".
In my case, I live in Japan, so "TimeZoneInfo.Local.StandardName" returns "Japan Standard Time".
On the other hand, "TimeZoneInfo.Local.Id" returns "Asia/Tokyo", which is the expected behavior.

On the other hand, the error that occurs with "ToEvent" is due to incorrect data being stored, so make the following temporary changes, delete the incorrect data, delete the comments and temporary code, and save again.

//var timezone = cursor.GetString(projection.IndexOf(CalendarContract.Events.InterfaceConsts.EventTimezone));
var timezone = TimeZoneInfo.Local.Id; // Delete this code when you are finished using it.

Now, try it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants