Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / May 2005

Tip: Looking for answers? Try searching our database.

DateTime Serialization Framework 1.1

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ns21 - 16 May 2005 11:21 GMT
Our application is windows desktop application. We are using VS.Net 2003, C#,
Framework 1.1, SQL 2000. We use webservices to add/update/select objects. We
are using XML Serialization. Following is a sample object at CLIENT

[System.Xml.Serialization.XmlAttributeAttribute()]
public System.DateTime ContactDateTime
{
  get { return _ContactDateTime; }
  set { _ContactDateTime = value; }
}

Following is the object at SERVER

[XmlAttribute]
public DateTime ContactDateTime
{
 get {return _contactDateTime;}
 set {_contactDateTime = value;}
}

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.

How this problem can be resolved?
Chad Z. Hower aka Kudzu - 16 May 2005 18:08 GMT
> 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 - 3 lines]
> 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
ns21 - 17 May 2005 07:25 GMT
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
William Stacey [MVP] - 17 May 2005 05:32 GMT
http://blogs.msdn.com/bclteam/archive/2004/05/21/136918.aspx

Signature

William Stacey [MVP]

> Our application is windows desktop application. We are using VS.Net 2003,
> C#,
> Framework 1.1, SQL 2000. We use webservices to add/update/select objects.
> We
> are using XML Serialization. Following is a sample object at CLIENT

...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.