.NET 3.5 中日期时间序列化的最佳实践 [英] Best practices for DateTime serialization in .NET 3.5

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

问题描述

大约 4 年前,我为 DateTime 关注了这篇MSDN文章在 .Net 1.1 和 ASMX Web 服务(使用 SQL 2000 服务器作为后端)上构建 .Net 客户端的最佳使用实践.我仍然记得我在使用 DateTime 时遇到的序列化问题以及它为不同时区的服务器所做的测试工作.

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.

我的问题是:对于 WCF 和 SQL Server 2008 等一些新技术,是否有类似的最佳实践文档,尤其是添加了用于存储时区感知信息的新日期时间类型.

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.

这是环境:

  1. 太平洋时间的 SQL Server 2008.
  2. 位于不同时区的 Web 服务层.
  3. 客户可能在不同的时区使用 .Net 2.0 或 .Net 3.5.如果方便的话,我们可以强制所有人升级到 .Net 3.5.:)

对于每一层要使用的数据类型有什么好的建议/最佳实践吗?

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

推荐答案

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

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.

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

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 中日期时间序列化的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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