Xamarin TimeZoneInfo.GetUtcOffset 抛出 NotImplementedException

Xamarin TimeZoneInfo.GetUtcOffset throws NotImplementedException

我正在尝试 运行 此代码:

_deviceTimeZone.GetUtcOffset(systemNow.ToDateTimeOffset()).TotalMilliseconds

但出现异常

Unhandled Exception: System.NotImplementedException: The method or operation is not implemented. at System.TimeZoneInfo.GetUtcOffset (DateTimeOffset dateTimeOffset) [0x00000] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/mcs/class/corlib/System/TimeZoneInfo.cs:669

我找到带有 GetUtcOffset 和 NotImplementedException 的单声道源:mono github

如果 TimeZoneInfo 无法执行此操作,我如何获取我的时区的 utc 偏移量?


固定:PR

mono 似乎确实实现了 GetUtcOffset(DateTime dateTime)。那你为什么不直接使用它呢:

_deviceTimeZone.GetUtcOffset(systemNow.ToDateTimeOffset().DateTime).TotalMilliseconds;

在@Übercoder 回答的帮助下,我找到了解决方案:

_deviceTimeZone.GetUtcOffset(systemNow.ToDateTimeOffset().UtcDateTime).TotalMilliseconds;

我使用 UtcDateTime 而不是 DateTime,因为 DateTime 忽略偏移量和 returns 'Unspecified' 时间,而 UtcDateTime 将 DateTimeOffset 转换为 UTC,并且我能够获得正确的时区偏移量。