All your DateTime(s) on the server should be stored and returned as UTC.
This has a few benifits. First, you have a stack in the ground as to what
format the dates are in (all UTC.) Second, they will serilize fine now as
they will be Zulu (hence the Z at the end). All your clients can then
convert to local time if needed or leave in UTC for datetime calcs. After
class is deserilized, you decide if you need to convert any/all DateTime
fields to LocalTime or leave in UTC.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
namespace NetNodes.DB
{
public class ClassWithDate
{
public ClassWithDate() { }
public DateTime Date;
public string Name;
public string ToXmlString()
{
string data = null;
XmlSerializer ser = new XmlSerializer(typeof(ClassWithDate));
using (StringWriter sw = new StringWriter())
{
ser.Serialize(sw, this);
sw.Flush();
data = sw.ToString();
return data;
}
}
public static ClassWithDate FromXml(string xmlString)
{
if (xmlString == null)
throw new ArgumentNullException("xmlString");
ClassWithDate cwd = null;
XmlSerializer ser = new XmlSerializer(typeof(ClassWithDate));
using (StringReader sr = new StringReader(xmlString))
{
cwd = (ClassWithDate)ser.Deserialize(sr);
}
return cwd;
}
}
}
private void button3_Click(object sender, EventArgs e)
{
// Serialize/Deserialize using UTC.
// Server side.
DateTime localDateTime = DateTime.Now;
Console.WriteLine("Server local Time: {0}",
localDateTime.ToString());
DateTime utcDateTime = localDateTime.ToUniversalTime();
Console.WriteLine("Server UTC Time: {0}\n",
utcDateTime.ToString());
ClassWithDate dc = new ClassWithDate();
dc.Name = "ABC";
dc.Date = DateTime.UtcNow;
string xml = dc.ToXmlString();
Console.WriteLine("Class on the wire per XmlSerializer (date as
UTC):");
Console.WriteLine(xml);
// Client side.
Console.WriteLine("\nClient Side:");
ClassWithDate ncwd = ClassWithDate.FromXml(xml);
Console.WriteLine("Server's UTC: {0}", ncwd.Date.ToString());
ncwd.Date = ncwd.Date.ToLocalTime();
Console.WriteLine("Server's UTC as my LocalTime: {0}",
ncwd.Date.ToString());
}
Example Output:
================================================== ====
Server local Time: 5/17/2005 5:26:21 PM
Server UTC Time: 5/17/2005 9:26:21 PM
Class on the wire per XmlSerializer (date as UTC):
<?xml version="1.0" encoding="utf-16"?>
<ClassWithDate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Date>2005-05-17T21:26:21.96875Z</Date>
<Name>ABC</Name>
</ClassWithDate>
Client Side:
Server's UTC: 5/17/2005 9:26:21 PM
Server's UTC as my LocalTime: 5/17/2005 5:26:21 PM
--
William Stacey [MVP]
"ns21" <ns**@microsoft.com> wrote in message
news:8B**********************************@microsof t.com...
Thanks Chad & William.
I am aware of the fact that changing the datetime to string is the
concrete
solution However I actually want to avoid changing the datetime types to
strings because These would be the huge changes in the application. I was
looking for the other ways to handle it.
Is this problem resolved in framework 2.0 / whidbey.
"Chad Z. Hower aka Kudzu" wrote:
"=?Utf-8?B?bnMyMQ==?=" <ns**@discussions.microsoft.com> wrote in
news:14**********************************@microsof t.com: > The problem is that when the client/server is in a timezone where
> dayLightSavings are ON. The DateTime field contains a value adjusted to
> one hour during Daylight saving period, which results in incorrect
> data.
> For ex: if the value in the database is "12/03/2005 23:00:00" when it
> is
> serialized and received at DLS client the date becomes "13/03/2005
> 00:00:00" same is the case when dates are serialized from client to
> server.
Its built into DateTime. You need to make your own value type, or what
most
users do is just use strings.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Blog: http://blogs.atozed.com/kudzu