【问题标题】:Using NodaTime, how to convert an Instant to the corresponding system's ZonedDateTime?使用 NodaTime,如何将 Instant 转换为对应系统的 ZonedDateTime?
【发布时间】:2013-12-28 05:05:45
【问题描述】:

我在数据库中有一个事件日志,事件的日期时间存储为 UTC 时间。在我使用 NodaTime 的应用程序中,我将值作为DateTime 获取,然后将其转换为Instant

var instant = NodaTime.Instant.FromDateTimeUtc(DateTime.SpecifyKind(eventDateTime, DateTimeKind.Utc));

现在我想使用系统的时区获取与instant 对应的ZonedDateTime。我知道我可以使用instant.InZone(systemTimeZone) 来获得我需要的ZonedDateTime。但是,我不知道如何用正确的值填充 systemTimeZone 变量。如何获取系统时区对应的DateTimeZone对象?

【问题讨论】:

    标签: c# .net nodatime


    【解决方案1】:

    使用IDateTimeZoneProvider.GetSystemDefault 作为您感兴趣的时区提供程序。例如:

    var systemZone = DateTimeZoneProviders.Tzdb.GetSystemDefault();
    var zonedDateTime = instant.InZone(systemZone);
    

    或使用DateTimeZoneProviders.Bcl 来使用适当的TimeZoneInfo 支持的时区。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      相关资源
      最近更新 更多