Problem with XML Webservice and timezone.

HELP !!!

I have a XML WebService on a machine with a WebMethod that returns a DataSet.
The dataset contains a DateTime field.
If I test the XML Service I see the following results in the web browser:
:
-<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">

-<NewDataSet xmlns="">
-<LogLine diffgr:id="LogLine1" msdata:rowOrder="0">
<Date_x002F_Time>2005-06-28T07:22:04.9730000+02:00</Date_x002F_Time>
:

This is correct. If I fetch the results on my PDA I got

"6/27/2005 11:22:04 PM"

(result of: string x = drAlarm["Date/Time"].ToString(); )

The timezone on the WebService machine = +1 on the PDA -6!!!

How can I get the raw datetime instead of the 'corrected datetime' on my PDA?
TIA

Leo Rozing

[3595 byte] By [DonLeo] at [2008-2-22]
# 1
This is a common issue in .NET Framework V1.0/1.1 also on the Compact Framework (which is a client)
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.

However if you are using Whidbey (which is currently in Beta), then this problem will be fixed. You can specify DataColum.DateTimeMode to be DataSetDateTime.UnSpecified and this will turn off these conversions between timezones and whole dates will be preserved.

MarkIhimoyan at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 2
Thanks Mark for the elaborate answer and by changing the column type I solved the problem.

Doing that I bumped into the problem of having the wrong Culture in my Web service. Do you have any idea how you change the current culture of a Web service? There seems to be no mean thread ...

Thanks a lot ...

DonLeo at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 3
Sorry it has taken a while to get to this.
This thread was marked as closed and as such did not show up in my query again...
Well I'm sure you might have figured this out by now... but you can easily set the Culture Info on the dataset returned from your web service by specifying the locale property of the dataset.
e.g
ds.Locale = CultureInfo.InvariantCulture
MarkIhimoyan at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...