Solved

Dataset from webservice - datetime columns are having offsets applied

Posted on 2009-04-14
2
537 Views
Last Modified: 2012-05-06
We have a client app which gets its data from a server. We use web services to run sql statements and a dataset is returned which contains the information.

The client app is used in different countries and the problem we have is that when the dataset is returned, all date columns are having timezone offset's applied. How can we stop this without having to modify the client application? Is there a setting inside the web service or server i can use instead? Changing regional/date settings on the client machine is completely out of the question.

At the moment, whenever we request a date column, we have to select it in sql like this; "Select convert(varchar, datecol) datecol from sometable"

Its not ideal...
0
Comment
Question by:tfsln
2 Comments
 
LVL 14

Accepted Solution

by:
GiftsonDJohn earned 500 total points
ID: 24144555
Hi,

This is a default behavior of .Net Frameworks.

See the following for some background on this:
http://blogs.msdn.com/brada/archive/2004/04/13/112784.aspx
http://msdn.microsoft.com/netframework/default.aspx?pull=/library/en-us/dndotnet/html/datetimecode.asp#datetime_topic4

The Xml serializer always assumes that DateTime values being serialized represent local machine time, so it applies the local time zone offset as the offset portion of the encoded XML time. When this is deserialized on the client, the original offset is subtracted from the value being parsed, and the current machine's time-zone offset is added. In order to get the "raw datetime" on your client, you will have to work around this.

See the following post for more on this:
http://blogs.msdn.com/bclteam/archive/2005/03/07/387677.aspx

Your options are:
1. Change the column type to be Int64 or String
2. Call DateTime.ToLocalTime on the DateTime before putting it in the DataSet and call DateTime.ToUniversalTime after taking it out. This effectively "cancels out" the adjustment and can be used whether you are dealing with a whole date or a UTC time.
3. Make all machines use the same time zone.
4. If you have a chance to pre-process the XML before it is sent out, you can manually strip out the time zone offset out of the XML text. For example, a typical XML date and time looks like this: 2005-01-28T03:14:42.0000000-07:00. You can use a Regex to remove the -07:00. You do not need to re-inject anything on the other end, as no adjustment is made if there is no time zone information. Do not try to replace the time zone offset with Z or +00:00. While technically a more correct representation, the existence of time zone information will cause the serializer to do an extra conversion to local.

Reference taken from
http://social.msdn.microsoft.com/Forums/en-US/netfxcompact/thread/7dfcf15a-825f-46db-a61a-8696833e7dcf

0
 

Author Closing Comment

by:tfsln
ID: 31570218
Great answer, thank you
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

To properly understand GitHub, let’s divide it into two words ‘Git’ and ‘Hub’. Git is basically a ‘Distribution Version Control’ (DVC) and ‘Source Code Management’ (SCM) system widely used by software programmers while Hub means the efficient centre…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question