DateTime Serialization Framework 1.1

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ns21

    DateTime Serialization Framework 1.1

    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.Seri alization.XmlAt tributeAttribut e()]
    public System.DateTime ContactDateTime
    {
    get { return _ContactDateTim e; }
    set { _ContactDateTim e = value; }
    }

    Following is the object at SERVER

    [XmlAttribute]
    public DateTime ContactDateTime
    {
    get {return _contactDateTim e;}
    set {_contactDateTi me = 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

    #2
    Re: DateTime Serialization Framework 1.1

    "=?Utf-8?B?bnMyMQ==?=" <ns21@discussio ns.microsoft.co m> wrote in
    news:140B8661-F828-449A-AF41-232E0D6F348C@mi crosoft.com:[color=blue]
    > 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.[/color]

    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/
    "Programmin g is an art form that fights back"

    Blog: http://blogs.atozed.com/kudzu

    Comment

    • William Stacey [MVP]

      #3
      Re: DateTime Serialization Framework 1.1



      --
      William Stacey [MVP]

      "ns21" <ns21@discussio ns.microsoft.co m> wrote in message
      news:140B8661-F828-449A-AF41-232E0D6F348C@mi crosoft.com...[color=blue]
      > 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
      >[/color]
      ....


      Comment

      • ns21

        #4
        Re: DateTime Serialization Framework 1.1

        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:
        [color=blue]
        > "=?Utf-8?B?bnMyMQ==?=" <ns21@discussio ns.microsoft.co m> wrote in
        > news:140B8661-F828-449A-AF41-232E0D6F348C@mi crosoft.com:[color=green]
        > > 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.[/color]
        >
        > 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/
        > "Programmin g is an art form that fights back"
        >
        > Blog: http://blogs.atozed.com/kudzu
        >[/color]

        Comment

        • Chad Z. Hower aka Kudzu

          #5
          Re: DateTime Serialization Framework 1.1

          "=?Utf-8?B?bnMyMQ==?=" <ns21@microsoft .com> wrote in
          news:8B84ABAF-00DA-4EDD-AD82-AB6995AD68DE@mi crosoft.com:[color=blue]
          > Is this problem resolved in framework 2.0 / whidbey.[/color]

          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/
          "Programmin g is an art form that fights back"

          Blog: http://blogs.atozed.com/kudzu

          Comment

          • William Stacey [MVP]

            #6
            Re: DateTime Serialization Framework 1.1

            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.Collecti ons.Generic;
            using System.Text;
            using System.IO;
            using System.Xml.Seri alization;

            namespace NetNodes.DB
            {
            public class ClassWithDate
            {
            public ClassWithDate() { }
            public DateTime Date;
            public string Name;
            public string ToXmlString()
            {
            string data = null;
            XmlSerializer ser = new XmlSerializer(t ypeof(ClassWith Date));
            using (StringWriter sw = new StringWriter())
            {
            ser.Serialize(s w, this);
            sw.Flush();
            data = sw.ToString();
            return data;
            }
            }
            public static ClassWithDate FromXml(string xmlString)
            {
            if (xmlString == null)
            throw new ArgumentNullExc eption("xmlStri ng");

            ClassWithDate cwd = null;
            XmlSerializer ser = new XmlSerializer(t ypeof(ClassWith Date));
            using (StringReader sr = new StringReader(xm lString))
            {
            cwd = (ClassWithDate) ser.Deserialize (sr);
            }
            return cwd;
            }
            }
            }

            private void button3_Click(o bject sender, EventArgs e)
            {
            // Serialize/Deserialize using UTC.
            // Server side.
            DateTime localDateTime = DateTime.Now;
            Console.WriteLi ne("Server local Time: {0}",
            localDateTime.T oString());
            DateTime utcDateTime = localDateTime.T oUniversalTime( );
            Console.WriteLi ne("Server UTC Time: {0}\n",
            utcDateTime.ToS tring());
            ClassWithDate dc = new ClassWithDate() ;
            dc.Name = "ABC";
            dc.Date = DateTime.UtcNow ;
            string xml = dc.ToXmlString( );
            Console.WriteLi ne("Class on the wire per XmlSerializer (date as
            UTC):");
            Console.WriteLi ne(xml);

            // Client side.
            Console.WriteLi ne("\nClient Side:");
            ClassWithDate ncwd = ClassWithDate.F romXml(xml);
            Console.WriteLi ne("Server's UTC: {0}", ncwd.Date.ToStr ing());
            ncwd.Date = ncwd.Date.ToLoc alTime();
            Console.WriteLi ne("Server's UTC as my LocalTime: {0}",
            ncwd.Date.ToStr ing());
            }

            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"?>
            <ClassWithDat e xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
            xmlns:xsd="http ://www.w3.org/2001/XMLSchema">
            <Date>2005-05-17T21:26:21.968 75Z</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" <ns21@microsoft .com> wrote in message
            news:8B84ABAF-00DA-4EDD-AD82-AB6995AD68DE@mi crosoft.com...[color=blue]
            > 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:
            >[color=green]
            >> "=?Utf-8?B?bnMyMQ==?=" <ns21@discussio ns.microsoft.co m> wrote in
            >> news:140B8661-F828-449A-AF41-232E0D6F348C@mi crosoft.com:[color=darkred]
            >> > 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.[/color]
            >>
            >> 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/
            >> "Programmin g is an art form that fights back"
            >>
            >> Blog: http://blogs.atozed.com/kudzu
            >>[/color][/color]


            Comment

            Working...