Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for DateTime serialization in .NET 3.5

Tags:

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?

like image 729
Gulzar Nazim Avatar asked Sep 15 '08 17:09

Gulzar Nazim


2 Answers

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.

like image 192
Abe Heidebrecht Avatar answered Sep 29 '22 22:09

Abe Heidebrecht


One big issue is that WCF serialization doesn't support xs:Date. This is a big problem as if all you want is a date, you shouldn't be forced to be concerned about time zones. The following connect issue discusses some of the problems: http://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=349215

If you want to represent a point in time unambiguously, i.e. not just the date part, you can use the DateTimeOffset class if you have .NET 3.5 on both client and server. Or for interoperability, always pass date/time values as UTC.

like image 41
Joe Avatar answered Sep 29 '22 22:09

Joe