.NET 3.5中DateTime序列化的最佳做法 [英] Best practices for DateTime serialization in .NET 3.5

查看:137
本文介绍了.NET 3.5中DateTime序列化的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大约4年前,我遵循了这个 MSDN文章 for DateTime用于在.Net 1.1和ASMX Web服务(以SQL 2000服务器为后端)上构建.Net客户端的最佳做法。我仍然记得我使用DateTime的序列化问题,以及在不同时区的服务器进行的测试工作。



我的问题是:是否有类似的最佳做法文档对于一些新技术,如WCF和SQL Server 2008,特别是添加了新的datetime类型来存储时区感知信息。



这是环境: p>


  1. 太平洋时间的SQL Server 2008。

  2. 不同时区的Web服务图层。 >
  3. 客户端可以在不同的时区使用.Net 2.0或.Net 3.5。如果这样做很容易,我们就可以迫使所有人升级到.Net 3.5。 :)

在每个图层中使用的数据类型的任何好的建议/最佳做法?

解决方案

我认为这样做的最好办法是始终将对象作为UTC传递,并在客户端上转换为本地时间。通过这样做,所有客户端都有一个共同的参考点。



要转换为UTC,请在DateTime对象上调用ToUniversalTime。然后,在客户端,调用ToLocalTime来获取当前时区。


Some 4 years back, I followed this MSDN article for DateTime usage best practices for building a .Net client on .Net 1.1 and ASMX web services (with SQL 2000 server as the backend). I still remember the serialization issues I had with DateTime and the testing effort it took for servers in different time zones.

My questions is this: Is there a similar best practices document for some of the new technologies like WCF and SQL server 2008, especially with the addition of new datetime types for storing time zone aware info.

This is the environment:

  1. SQL server 2008 on Pacific Time.
  2. Web Services layer on a different time zone.
  3. Clients could be using .Net 2.0 or .Net 3.5 on different time zones. If it makes it easy, we can force everyone to upgrade to .Net 3.5. :)

Any good suggestions/best practices for the data types to be used in each layer?

解决方案

I think the best way of doing this is to always pass the object as UTC, and convert to local time on the clients. By doing so, there is a common reference point for all clients.

To convert to UTC, call ToUniversalTime on the DateTime object. Then, on the clients, call ToLocalTime to get it in their current time zone.

这篇关于.NET 3.5中DateTime序列化的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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