468,257 Members | 1,299 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

DateTime, serialized xml and timezones

Hi,

Please bear with me on this problem, first I'll give you some background:

I have an object that contains a DateTime field which i pass to a webservice

public class Foo {
public DateTime RequestDate;
}

public class FooWebService : System.Web.Services.WebService {
[WebMethod]
public void FooWebMethod(Foo value) {
// Do something
}
}

On a client app i can then consume this webserivce like this:

localhost.Foo f = new localhost.Foo();
f.RequestDate = DateTime.Parse("27/5/2003");
localhost.FooWebService service = new localhost.FooWebService();
service.FooWebMethod(f);

Now the .Net framework handles the serialization of the Foo object into xml
and then passes it to the webservice, the object is the rehydrated by the
framework. I can see xml element for the RequestDate field has a value
(namespace uri omitted)

<Foo>
<RequestDate>2003-05-27T00:00:00.0000000+10:00</RequestDate>
</Foo>

The problem occurs when the client and the webservice reside in different
timezones. My client is in Sydney (GMT+10) whereas the webservice is in
Adelaide (GMT+9:30). When the Foo object is rehydrated by the framework it
converts the serialized xml date string into the DateTime value '26/5/2003'
rather than '27/5/2003', I assume because its adjusting for the half hour
time difference. Is there anyway to stop this behaviour ?

tia
andrew


Nov 11 '05 #1
2 13281
Hi Andrew,

I haven't seen any way to change this behavior, but there is a way to
mitigate it. It requires a small code change on both the client and the web
service. You need to have the web service delivery every DateTime value in
UTC format, and make sure every client application gives you DateTime values
in UTC format. Then, in your web service, before you do anything with the
DateTime values, you convert them to your local time. Each client app
should also change the DateTime value back to the local time for the client.

The syntax for going from one format to the other is:

System.DateTime univDateTime = localDateTime.ToUniversalTime();
System.DateTime localDateTime = univDateTime.ToLocalTime();

By doing this, at least all the date time processing in your web service is
done on values with a common format.

I hope this helps.

Brooks Stoner

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.

"andrew lowe" <andrew [dot] lowe [at] geac [.] com> wrote in message
news:up**************@TK2MSFTNGP10.phx.gbl...
Hi,

Please bear with me on this problem, first I'll give you some background:

I have an object that contains a DateTime field which i pass to a webservice
public class Foo {
public DateTime RequestDate;
}

public class FooWebService : System.Web.Services.WebService {
[WebMethod]
public void FooWebMethod(Foo value) {
// Do something
}
}

On a client app i can then consume this webserivce like this:

localhost.Foo f = new localhost.Foo();
f.RequestDate = DateTime.Parse("27/5/2003");
localhost.FooWebService service = new localhost.FooWebService();
service.FooWebMethod(f);

Now the .Net framework handles the serialization of the Foo object into xml and then passes it to the webservice, the object is the rehydrated by the
framework. I can see xml element for the RequestDate field has a value
(namespace uri omitted)

<Foo>
<RequestDate>2003-05-27T00:00:00.0000000+10:00</RequestDate>
</Foo>

The problem occurs when the client and the webservice reside in different
timezones. My client is in Sydney (GMT+10) whereas the webservice is in
Adelaide (GMT+9:30). When the Foo object is rehydrated by the framework it converts the serialized xml date string into the DateTime value '26/5/2003' rather than '27/5/2003', I assume because its adjusting for the half hour
time difference. Is there anyway to stop this behaviour ?

tia
andrew

Nov 11 '05 #2
Hello Andrew,
Is there anyway to stop this behaviour ?
What is it that you want to happen?
It seems to me that if you de-serialize
<RequestDate>2003-05-27T00:00:00.0000000+10:00</RequestDate>

in a timezone that is not +10, you get the same time, just expressed
differently. For example if I try it, then serialize the result, I get
<Foo>
<RequestDate>2003-05-26T10:00:00.0000000-04:00</RequestDate>
</Foo>

Which is the exact same time as the original.
If you want to just avoid the entire issue, you can serialize and then
transmit your DateTime as a date.
You can do this using an attribute for System.Xml.Serialization.
An example illustrating all this follows.

Other discussions on this topic:
http://www.dotnet247.com/247referenc...25/125458.aspx

----
Dino Chiesa
Microsoft Developer Division
d i n o c h @ o n l i n e . m i c r o s o f t . c o m

================================================== ==
using System.Xml.Serialization;

namespace Ionic {

public class Foo {
[XmlElement()]
public System.DateTime RequestDate;
}

public class SuppressTimeZoneInfo {
[XmlElement(DataType="date")] // "date" refers to the XML Schema
datatype
public System.DateTime RequestDate;
}
public class TestDriver {

/// <remarks>
/// Converts a string to a MemoryStream.
/// </remarks>
static System.IO.MemoryStream StringToMemoryStream(string s) {
System.Text.ASCIIEncoding enc= new System.Text.ASCIIEncoding();
int byteCount = enc.GetByteCount(s.ToCharArray(), 0, s.Length);
byte[] ByteArray=new byte[byteCount];
int bytesEncodedCount = enc.GetBytes(s, 0, s.Length, ByteArray, 0);
System.IO.MemoryStream ms = new System.IO.MemoryStream(ByteArray);
return ms;
}

static void Main(string[] args) {

XmlSerializer s = new XmlSerializer(typeof(Foo));
string OrigHoursOffset= "10";
string OriginalXml=
"<Foo>\n" +
" <RequestDate>2003-05-27T00:00:00.0000000+" + OrigHoursOffset +
":00</RequestDate>\n" +
"</Foo>\n";

System.Console.WriteLine("\nOriginal XML:\n{0} \n\nDe-serialized
locally:", OriginalXml);
System.IO.MemoryStream ms = StringToMemoryStream(OriginalXml);
// slurp it in
Foo f1= (Foo) s.Deserialize(ms);

// use this to "suppress" the default xsd and xsd-instance namespaces
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add( "", "" );
// spit it out
s.Serialize(System.Console.Out, f1, ns);

// now, some other options...
System.Console.WriteLine("\n\nUTC form:\n{0}\n",
f1.RequestDate.ToUniversalTime().ToString("s"));
int HoursOffsetFromUtc=
System.TimeZone.CurrentTimeZone.GetUtcOffset(Syste m.DateTime.Now).Hours;

System.Console.WriteLine("Hours local time is offset from UTC: {0}\n",
HoursOffsetFromUtc.ToString());

int HoursToAdd= System.Int32.Parse(OrigHoursOffset) -
HoursOffsetFromUtc;
System.DateTime newtime= f1.RequestDate.AddHours( HoursToAdd );
System.Console.WriteLine("Adding {0} hours...\nnew (modified) time:
{1}\n", HoursToAdd, newtime.ToString("s"));

f1.RequestDate= newtime;
System.Console.WriteLine("\nSerializing the modified time...\n");
s.Serialize(System.Console.Out, f1, ns);
System.Console.WriteLine("\n\n\nSuppressing TZ and time info:\n");

SuppressTimeZoneInfo f2= new SuppressTimeZoneInfo();
f2.RequestDate= f1.RequestDate;
XmlSerializer s2 = new XmlSerializer(typeof(SuppressTimeZoneInfo));
s2.Serialize(System.Console.Out, f2, ns);
System.Console.WriteLine("\n");

}
}
}

"andrew lowe" <andrew [dot] lowe [at] geac [.] com> wrote in message
news:up**************@TK2MSFTNGP10.phx.gbl... Hi,

Please bear with me on this problem, first I'll give you some background:

I have an object that contains a DateTime field which i pass to a webservice
public class Foo {
public DateTime RequestDate;
}

public class FooWebService : System.Web.Services.WebService {
[WebMethod]
public void FooWebMethod(Foo value) {
// Do something
}
}

On a client app i can then consume this webserivce like this:

localhost.Foo f = new localhost.Foo();
f.RequestDate = DateTime.Parse("27/5/2003");
localhost.FooWebService service = new localhost.FooWebService();
service.FooWebMethod(f);

Now the .Net framework handles the serialization of the Foo object into xml and then passes it to the webservice, the object is the rehydrated by the
framework. I can see xml element for the RequestDate field has a value
(namespace uri omitted)

<Foo>
<RequestDate>2003-05-27T00:00:00.0000000+10:00</RequestDate>
</Foo>

The problem occurs when the client and the webservice reside in different
timezones. My client is in Sydney (GMT+10) whereas the webservice is in
Adelaide (GMT+9:30). When the Foo object is rehydrated by the framework it converts the serialized xml date string into the DateTime value '26/5/2003' rather than '27/5/2003', I assume because its adjusting for the half hour
time difference. Is there anyway to stop this behaviour ?

tia
andrew

Nov 11 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Navin Mishra | last post: by
9 posts views Thread by Phil B | last post: by
9 posts views Thread by piersh | last post: by
3 posts views Thread by Zachary Turner | last post: by
9 posts views Thread by Abhishek | last post: by
7 posts views Thread by Looch | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.