Solved

Dataset from webservice - datetime columns are having offsets applied

Posted on 2009-04-14
2
521 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

New Relic recently released its Synthetics product that allows for the creation of performance monitors that periodically test a site's performance. If you wish to test an interactive workflow New Relic employs Selenium WebDriverJS to run those test…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now