从本地服务器迁移到 Azure:TIMEZONE UTC。怎么解决?
我设计我的应用程序时考虑到了这样一个事实:根据规范, 应该在位于意大利的服务器上运行,并且客户端只能是意大利人。
大约一个月前,我的老板决定将所有内容都放在 Azure 上。
一切都很顺利。唯一给我带来问题的是时间服务器是 UTC。
解决办法是:
A)简单
修改启动脚本为服务器时间( http://netindonesia.net /blogs/wely/archive/2011/06/26/setting-timezone-in-windows-azure.aspx )
B) 更费力
进行更改以使任何应用程序使用 UTC 并显示正确转换为本地时间的时间。
如果我选择解决方案 A,我的疑问是,服务器设置不同时区的事实可能会以某种方式与 Azure 产生冲突。
这是真实的?
I designed my application considering the fact that, according to the specifications,
should run on a server located in Italy and clients will be italian people only.
About a month ago, my bos decided to bring it all on Azure.
Everything went smoothly. The only thing that give me some problem is the fact that the time server is UTC.
The solutions are:
A) simple
modify a startup script to the server time (
http://netindonesia.net/blogs/wely/archive/2011/06/26/setting-timezone-in-windows-azure.aspx
)
B) more laborious
Change to make any application to use UTC and show the time properly converted to local time.
If i go for the solution A my doubt is that the fact that the server is set up with a different timezone can somehow create conflicts with Azure.
This is true?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经要求MS支持了。
这是响应:
不建议使用启动任务更改 Azure 虚拟机上的服务器时间,您应该在代码中使用 TimeZoneInfo.ConvertTimeFromUTCTime 等方法。
所以我不会更改服务器时区。
在等待支持人员的回复时,我发现 SqlServer 2008 有一个完美的 DateTimeOffset 数据类型!
http://blogs.msdn.com/b/davidrickard/archive/2012/04/07/system-datetime-good-practices-and-common-pitfalls.aspx
I have asked to MS support.
This is the response:
Changing the server time on the Azure Virtual Machines using a startup task is not recommended, you should rather use methods like TimeZoneInfo.ConvertTimeFromUTCTime in your code.
So I will not change the server timezone.
Waiting for a response from the support I discover that SqlServer 2008 have a DateTimeOffset data type that is perfect!
http://blogs.msdn.com/b/davidrickard/archive/2012/04/07/system-datetime-good-practices-and-common-pitfalls.aspx
几年前,我开始设计所有应用程序,以便在客户端和服务器上使用 UTC 处理日期,此后就再也没有回头过。
A couple years ago I have begun designing all of my apps to consequently handle dates using UTC on both client and server and haven't looked back since.
DateTime.Now 和 DateTime.Today 在客户端和服务器端存在两个问题。
当您将 DateTime 对象从客户端传递到 Azure 时,其 Kind 等于 Local 并且包含时区信息。
(2011年6月10日,12:30am -7)
但是,当您将其保存到数据库时,区域信息会丢失。随后,当从数据库读取此字段时,它会创建带有 Utc 区域的 DateTime (June 10, 2011, 12:30am 0)
最终,您的客户端会错误地读取日期时间。
有多种选项可以在客户端解决此问题。
1) 将方法参数和数据库中的 DateTime 转换为 DateTimeOffset。这将保证您的本地区域(即 PST)将保存在数据库中
2)使用 DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified) - 这样,日期时间的类型是未指定的,并且随后按原样保存在数据库中。
请小心在服务器端调用 DateTime.Now。您最好使用 DateTime.UtcNow。这个时间不应该用于业务数据。理想情况下,您需要重构代码并从客户端传递 DateTime 或 DateTimeOffset。
There are two issues with DateTime.Now and DateTime.Today on a client side and server side.
When you pass DateTime object from client to Azure its Kind equals to Local and it contains time zone information.
(June 10, 2011, 12:30am -7)
However, when you save it to the database the region information is lost. Subsequently, when reading this field from the database it creates DateTime with a Utc region (June 10, 2011, 12:30am 0)
Eventually, your client reads the datetime incorrectly.
There are several options to resolve this issue on a client side.
1) Convert DateTime to DateTimeOffset in Method parameters as well as in database. This will guarantee that your Local region (ie PST) will be saved in db
2) Use DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified) - this way the kind of DateTime is unspecified and subsequently saved as is in the db.
Be careful to call DateTime.Now on a server side. You better use DateTime.UtcNow. This time shouldn't be used for business data. Ideally, you would need to refactor your code and pass DateTime or DateTimeOffset from the client.