Azure 中的 C# DateTime.Now 不报告 GMT - 可以设置默认时区吗?

C# DateTime.Now in Azure not reporting GMT - can the default timezone be set?

在 Azure 网站中,DateTime.Now 总是返回 UTC 时间,我需要能够将时区设置为 GMT,所以目前所有时间都是一个小时。

此时我不能更改任何代码,所以问题是默认时区是否可以在配置中或以编程方式更改(可以在 Global.asax 中设置默认文化以确保日期格式正确)。

首先,GMT 与 UTC 基本相同 - it has no offset. To be precise, UTC supersedes GMT。您可能将它与夏季时间 GMT+1:00 的 BST 混淆了。

一般来说,对时区做出假设是个坏主意,尤其是在 Web 应用程序中。您应该使用 DateTimeOffset 而不是 DateTime。

幸运的是,Azure 网站最近发生了变化,现在您可以可以 change the timezone of an Azure Web site 以受支持且可靠的方式进行。来自 MSDN 博客 post

All you need to do is add an Application Setting (via the portal or the management APIs) called WEBSITE_TIME_ZONE and set that to the name of the time zone as defined in the Windows Registry under HKLM\Software\Microsoft\Windows Nt\CurrentVersion\Time Zones\ (for example, “AUS Eastern Standard Time”).

不幸的是,Windows 混淆了时区并使用名称 "GMT Standard Time" 来指代 BST,使用 "UTC" 来指代 GMT/UTC。