Timezone always returns a TimeZoneNotFoundException

I have code that works fine in an emulator, but on a 3gs device it crashes using a TimeZoneNotFoundException.

The reason for my code is to convert the server time (given in CET) to the local time zone.

var t = TimeZoneInfo.FindSystemTimeZoneById(TimeZone.CurrentTimeZone.StandardName); 
//This line below crashes               
double localOffset = t.GetUtcOffset(DateTime.Now).TotalMilliseconds;

The local time zone in my case is CET.

Any idea why this is not working in Monotouch? Is this a MonoTouch limitation? Since the code works in the emulator, I do not see that this is a programming error? I tried to rewrite this, but always end up with the same exception. Also tried "TimeZoneInfo.Local", but this immediately gives an error.

Any help is much appreciated!

+3
source share
4 answers

This was a bug in MonoTouch.

iOS , (, UNIX- ). iOS, TimeZoneNotFoundException.

MonoTouch (5.1.x iirc) NSTimeZone, .NET TimeZoneInfo , iOS, .

+2

, - , Newtonsoft.Json DateTime ( UTC.) , TimeZoneInfo.Local, ( .)

+1

. newtonsoft JSON DateTimeUtils.cs

Modify the GetLocalOffset function (this DateTime d function) to just use utcOffset = TimeZoneInfo.Local.GetUtcOffset (d);

instead of TimeZone.Local

+1
source

I had the same problem with device assemblies. I solved this by setting this attribute (on the mobile side). Serialization is now beautiful.

[JsonConverter (TypeOf (IsoDateTimeConverter))]
public DateTime? MyEndTime {get; set; }

Enjoy it!

+1
source

Source: https://habr.com/ru/post/1776755/


All Articles