Xamarin TimeZoneInfo.GetUtcOffset throws NotImplementedExcep
本文关键字:NotImplementedExcep throws GetUtcOffset TimeZoneInfo Xamarin | 更新日期: 2023-09-27 18:34:33
我正在尝试运行以下代码:
_deviceTimeZone.GetUtcOffset(systemNow.ToDateTimeOffset()).TotalMilliseconds
但有一个例外
未处理的异常:系统未实现异常:方法或 未实现操作。at System.TimeZoneInfo.GetUtcOffset (日期时间偏移日期时间偏移量([0x00000] 在 /Users/builder/data/lanes/2098/3efa14c4/source/mono/mcs/class/corlib/System/TimeZoneInfo.cs:669
如果时区信息无法做到这一点,我如何获得我的时区的 UTC 偏移量?
固定:公关
单
声道似乎确实实现了GetUtcOffset(DateTime dateTime)
。那么,您为什么不直接使用它呢:
_deviceTimeZone.GetUtcOffset(systemNow.ToDateTimeOffset().DateTime).TotalMilliseconds;
在@Übercoder答案的帮助下,我找到了解决方案:
_deviceTimeZone.GetUtcOffset(systemNow.ToDateTimeOffset().UtcDateTime).TotalMilliseconds;
我使用 UtcDateTime 而不是 DateTime,因为 DateTime 忽略偏移量并返回"未指定"时间,而 UtcDateTime 将 DateTimeOffset 转换为 UTC,并且我能够获得正确的时区偏移量。