Azure 中的时区偏移不正确

发布于 2025-01-08 02:13:35 字数 1599 浏览 0 评论 0原文

更新 - 问题不在于扩展方法

事实证明,我的扩展方法没有实际问题。当我像 DateTime.UtcNow.ToUserLocalTime() 一样直接从网络服务器向它提供 DateTime 时,我意识到该方法实际上产生了正确的结果。问题是我不小心在显示不正确的日期时间上调用了它两次 - 首先在控制器中,然后再次在视图中。这是一个非常愚蠢的错误,在我把头撞到墙上几个小时后才意识到。从现在开始,仅从控制器调用它。

我在 Azure 中运行的 ASP.Net MVC3 应用程序中遇到时区转换问题。当我在本地运行该应用程序时,一切都工作正常,但是当我在 Azure 中运行完全相同的代码时,它最终会关闭 1 个小时。

我为 DateTime 创建了一个扩展方法,用于根据用户选择的时区转换 DateTime。所有日期时间均以 UTC 格式存储。

public static DateTime ToUserLocalTime(this DateTime date)
{
    date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
    if (!string.IsNullOrEmpty(SecurityContext.CurrentUser.TimeZone))
    {
        var timezone = SecurityContext.CurrentUser.TimeZone;
        var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timezone);
        return TimeZoneInfo.ConvertTime(date, timeZoneInfo);
    }

    return date;
}

SecurityContext.CurrentUser.TimeZone 是时区的字符串表示形式,在我的例子中 W.欧洲标准时间

当我从本地 SQL 数据库获取 DateTime 2011-12-02 13:46:22 并通过我的 ToUserLocalTime 方法运行它时,将其转换为 W.欧洲标准时间 在我的本地环境中,我得到的 2011-12-02 14:46:22 这是正确的。但是,当我在 Azure 中运行完全相同的代码时,从 SQL-Azure 数据库中读取数据(其中包含完全相同的日期时间和所选时区),我得到 2011-12-02 15:46:22。这让我很困惑,为什么最后休息了一个小时?

我真的不知道如何调试它,因为我无法在本地重现它。有没有办法在 Azure 角色中对此进行调试,以便我可以看到代码中到底发生了什么?类似于 Visual Studio 中的调试器?

我尝试在发布应用程序时启用 IntelliTrace,但是当我这样做时,应用程序无法启动,并在连接到 Azure-SQL 数据库时抱怨连接错误。我没有 IntelliTrace 经验,生成的日志能否告诉我有关此处发生的情况的任何信息?在这种情况下,值得花时间让 InteliTrace 发挥作用吗?

任何关于这里可能出现问题或如何调试它的想法将不胜感激。

Update - the problem didn't lie within the extension method

As it turned out, there was no actual problem with my extension method. When I feed it with a DateTime straight from the webserver like so DateTime.UtcNow.ToUserLocalTime() I realized that the method was actually producing the correct result. The problem was instead that I accidentally called it twice on the DateTimes that were displayed incorrectly - first in the controller and then again in the view. A really stupid mistake, realized after beating my head against the wall for hours. From now on it is called from the controller only.

I have a problem with a time zone conversion in an ASP.Net MVC3 application running in Azure. When I run the application locally, everything is working just fine, however when I run the exact same code in Azure, it ends up +1 hour off.

I have created an extension method for DateTime that I use to convert the DateTime according to the time zone chosen by the user. All DateTimes are stored in UTC.

public static DateTime ToUserLocalTime(this DateTime date)
{
    date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
    if (!string.IsNullOrEmpty(SecurityContext.CurrentUser.TimeZone))
    {
        var timezone = SecurityContext.CurrentUser.TimeZone;
        var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timezone);
        return TimeZoneInfo.ConvertTime(date, timeZoneInfo);
    }

    return date;
}

SecurityContext.CurrentUser.TimeZone is a string-representation of the time zone, in my case W. Europe Standard Time.

When I get the DateTime 2011-12-02 13:46:22 from my local SQL-database and run it through my ToUserLocalTime method converting it to W. Europe Standard Time in my local environment I get 2011-12-02 14:46:22 which is correct. However when I run the exact same code in Azure, reading from my SQL-Azure db, which contains the exact same DateTime and chosen time zone I get 2011-12-02 15:46:22. This baffles me, why does it end up one hour off?

I really don't know how to debug this, since I cannot reproduce it locally. Is there a way to debug this in the Azure role, so that I can see what is really going on in the code? Something like the debugger in Visual Studio?

I tried to enable IntelliTrace when I publish the application, but when I did the application wouldn't start, complaining about a connection-error when connecting to the Azure-SQL db. I have no experience of IntelliTrace, would the generated logs be able to tell me anything about what is going on here? Would it be worth spending time getting InteliTrace to work in this case?

Any ideas on what might be wrong here, or how to debug it would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁的年少不轻狂 2025-01-15 02:13:35

参考怎么样

return TimeZoneInfo.ConvertTimeFromUTCTime(date, timeZoneInfo);

使用而不是

return TimeZoneInfo.ConvertTime(date, timeZoneInfo);

http://social.msdn.microsoft.com/Forums/en-AU/windowsazuredevelopment/thread/04be9d07-fcdb-4bf2-b380-57c062382409

然后您可以删除该行 date = DateTime.SpecifyKind(date, DateTimeKind.Utc);,假设您的日期始终具有未指定类型

或者可能是夏令时把事情搞砸了?请参阅http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/92696af4-5169-4015-90eb-79da3c5136c2/

要测试此功能,请尝试转到该页面通过单击计算机时钟和“更改日期和时间设置”来更改时区。然后取消选中/选中自动调整夏令时时间。

What about using

return TimeZoneInfo.ConvertTimeFromUTCTime(date, timeZoneInfo);

instead of

return TimeZoneInfo.ConvertTime(date, timeZoneInfo);

Reference: http://social.msdn.microsoft.com/Forums/en-AU/windowsazuredevelopment/thread/04be9d07-fcdb-4bf2-b380-57c062382409

You could then remove the line date = DateTime.SpecifyKind(date, DateTimeKind.Utc);, assuming that your date always has the Unspecified kind

OR

Or could it be Daylight Saving messing things up? See http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/92696af4-5169-4015-90eb-79da3c5136c2/

To test this, try going the page that changes your timezone, by clicking on your computer clock and "Change date and time settings. Then uncheck/check the Automatically adjust time for Daylight Saving.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文