【问题标题】:Could not convert utc time to specific time zone [duplicate]无法将 UTC 时间转换为特定时区 [重复]
【发布时间】:2021-07-13 14:27:47
【问题描述】:

我已经在 azure linux 中部署了我的 asp.net 核心应用程序。我正在尝试将 UTC 时间转换为“中欧标准时间”。 但是,我收到以下异常。在我的本地没有问题,但部署后我遇到了问题。

在 本地计算机。找不到文件'/usr/share/zoneinfo/Central 欧洲标准时间”。

TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time");
   var cstTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, cstZone);

【问题讨论】:

  • 那个时区是特定于 Windows 的,您需要使用 IANA 名称。
  • 请注意Central European Standard Time vs Central Europe Standard Time

标签: c# azure asp.net-core


【解决方案1】:

对于在 Linux 上运行的应用服务,您应该使用 IANA TZ 数据库中的时区值。

在你的情况下,Linux 中的时区Central Europe Standard Time 可能是Europe/Amsterdam

https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

https://docs.microsoft.com/en-us/azure/app-service/faq-configuration-and-management#how-do-i-set-the-server-time-zone-for-my-web-app

【讨论】:

【解决方案2】:

Linux 和 Windows 以不同的方式存储时区(参见例如 TimeZoneInfo in .NET Core when hosting on unix (nginx))。

一种解决方案是检查可用的内容并适当地选择,如How to get TimeZoneInfo on Windows and Linux from www.ankursheel.com 所示:

var nzTimeZone = TimeZoneInfo.GetSystemTimeZones().Any(x => x.Id == "New Zealand Standard Time")
   ? TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time")
   : TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland");

【讨论】:

猜你喜欢
  • 2020-03-21
  • 2018-03-24
  • 1970-01-01
  • 2021-01-04
  • 1970-01-01
  • 2020-06-16
  • 2014-08-12
  • 2014-07-15
  • 1970-01-01
相关资源
最近更新 更多