【问题标题】:Xamarin TimeZoneInfo.GetUtcOffset throws NotImplementedExceptionXamarin TimeZoneInfo.GetUtcOffset 抛出 NotImplementedException
【发布时间】:2016-05-26 00:50:03
【问题描述】:

我正在尝试运行此代码:

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

但有异常

未处理的异常:System.NotImplementedException:方法或 操作未执行。在 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

【问题讨论】:

    标签: c# xamarin mono


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      我使用 UtcDateTime 而不是 DateTime,因为 DateTime 忽略偏移并返回“未指定”时间,而 UtcDateTime 将 DateTimeOffset 转换为 UTC,我能够获得正确的时区偏移。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-30
        • 1970-01-01
        • 2019-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多