473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 14164
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Symon R | last post by:
This is a bit of a weird one that I haven't yet been able to solve - I'm hoping someone out there can disprove my findings and tell me where I've gone wrong! I have designed a web service that...
2
by: Navin Mishra | last post by:
Hi, Can a web service method return safely a DateTime value ? Would it cause problems for non-.NET client proxy generators ? Thanks Navin
9
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope...
9
by: piersh | last post by:
Does anyone know how to make the following method work across timezones? public DateTime GetTime () { return DateTime.UtcNow; }
0
by: rlaemmler | last post by:
Hi, I just migrated my web app to .NET 2.0. Part of the app creates some business objects from a MySQL query which is returned by a web service. Some of those objects contain DateTime...
3
by: Zachary Turner | last post by:
Hello, I have a situation where I would like to perform custom serialization and deserialization of an existing .NET framework object (specifically, System.DateTime). Is there a common paradigm...
4
by: Michael Meckelein | last post by:
Hello, Wondering, if C# (framework 2.0) does not support parsing DateTime timezones in three letter acronyms. I would like to parse date strings like "2005 Nov 01 11:58:47.490 CST -6:00" but...
9
by: Abhishek | last post by:
Hi I am trying to deserialize/ Parse a datetime object with the below string "2007-05-14T08:00:00.000+02:30" . If i am in GMT + 2.30 time zone everything's fine however if i am in GMT + 5.30 i...
7
by: Looch | last post by:
All, I'm using a WinForm app that calls methods on a remotable object. The app server hosting the object is in New York. Using the same exact application, a Sql Server 2005 datetime column's...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.