.NET Web服务中的自动时区转换 [英] Automatic timezone conversion in .NET web services

查看:92
本文介绍了.NET Web服务中的自动时区转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

当我们将DateTime数据类型传递给.NET Web服务时,或者当DataTable中的数据返回给客户端时,所有DateTime字段都将转换为与目标时区匹配.例如,如果服务器在东部时间(GMT-5)运行,而客户端在印度时间(GMT + 5:30)运行,并且我们将6:00 PM传递给服务器,则该时间在服务器上反映为7 :30 AM.尽管在大多数情况下这是可以的,因为两个时间都表示相同的时间点,但是当我们想与数据库中的字段进行比较时,这是非常不希望的,等等.我在谷歌搜索中遇到的一个解决方案是将值存储为数据库中的UTC,但是当我们将Web服务集成到现有系统中时,这是不切实际的.我有自己的方法来解决此问题(使用一些时间函数等),但是我想知道.NET(最好是.NET 2.0)是否内置了某些功能来解决此问题.

在此先感谢

Hi All,

When we pass a DateTime data type to a .NET web service or when data is returned in a DataTable back to the client, all the DateTime fields are converted to match the destination timezone. For example, if the server runs in Eastern Time (GMT-5) and the Client runs in Indian Time (GMT+5:30), and we pass 6:00 PM to the server, the time is reflected at the server as 7:30 AM. Whereas this is okay in most situations because both times represent the same point in time, it is highly undesirable when we want to make a comparisons with a field in the database, etc. One solution I have come across on googling this is storing values as UTC in database, but this is not practical when we integrate web service into existing systems. I have my own ways of dealing with this issue (using some time functions, etc,), but i would like to know if there is something built into .NET (preferably .NET 2.0) to get around with this.

Thanks in advance

推荐答案

您应该始终将时间值存储为UTC.绝对没有理由为什么您需要将它们存储为其他任何内容.只有这样,您才能确保可以在世界任何地方为任何用户正确显示该值.
You should always store time values as UTC. There is absolutely no reason why you would ever need to store them as anything else. Only in that way can you be sure that the value can be displayed correctly for any user, in any part of the world.


http://stackoverflow.com/a/6935884/ has the best answer I''ve seen. DataColumn has a DateTimeMode member. If you change it from UnspecifiedLocal (the default?) to Unspecified, .NET quits sending the time zone. This works great for us. YMMV


这篇关于.NET Web服务中的自动时区转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆