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.
> > The problem is that when the client/server is in a timezone where
> > dayLightSavings are ON. The DateTime field contains a value adjusted to
[quoted text clipped - 12 lines]
>
> Blog: http://blogs.atozed.com/kudzu
Chad Z. Hower aka Kudzu - 17 May 2005 16:24 GMT
> Is this problem resolved in framework 2.0 / whidbey.
Its actually a feature. It can be a big PITA, but it can be very useful too.
Its more problematic when you are storing dates, or a computer has the wrong
time zone set.
--
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
William Stacey [MVP] - 17 May 2005 22:28 GMT
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

Signature
William Stacey [MVP]
> Thanks Chad & William.
> I am aware of the fact that changing the datetime to string is the
[quoted text clipped - 24 lines]
>>
>> Blog: http://blogs.atozed.com/kudzu