[Mono-dev] Bug in handling of Datetime in Xml Serialization - testcase in msg

Mads Bondo Dydensborg mbd at dbc.dk
Mon Jan 7 07:51:29 EST 2008


Hi there

I have a difference between output in current Mono SVN and Windows 2.xxx. The 
following program construcs an object containing a date from an xml string, 
serializes and deserializes a couple of times. On Windows, the output remains 
stable, on Mono/Linux, my timezone appears to be substracted once for each 
serialization/deserialization call.

Here is the program:

---------------

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public class test {
  public System.DateTime date;
}

public class Test {

  public static string DeSe( string doc ) {
    // Deserialize
    XmlSerializer serializer = new XmlSerializer( typeof ( test ) );
    StringReader sr = new StringReader( doc );
    XmlReader reader = XmlReader.Create( sr );
    test t     = (test) serializer.Deserialize( reader );
    reader.Close();
    sr.Close();
    
    // Serialize
    MemoryStream memStream         = new MemoryStream();
    XmlTextWriter writer           = new XmlTextWriter( memStream, 
Encoding.UTF8 );
    serializer.Serialize( writer, t );
    writer.Close();
    memStream.Close();

    string xml;
    xml = Encoding.UTF8.GetString(memStream.GetBuffer());
    xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
    xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
    return xml;
  }

  public static void Main() {
    // Start with 10:30, no timezone
    string doc = @"<test><date>2007-06-15T10:30:10.5</date></test>";
    Console.WriteLine( "doc   = '{0}'", doc );
      
    string docm = DeSe( doc );
    Console.WriteLine( "docm  = '{0}'", docm );
      
    string doc2m = DeSe( docm );
    Console.WriteLine( "doc2m = '{0}'", doc2m );

    string doc3m = DeSe( doc2m );
    Console.WriteLine( "doc3m = '{0}'", doc3m );
  }
}        

----------

Here is the ouput from Windows:
doc   = '<test><date>2007-06-15T10:30:10.5</date></test>'
docm  = '<?xml version="1.0" encoding="utf-8"?><test 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><date>2007-06-15T10:30:10.5</date></test>'
doc2m = '<?xml version="1.0" encoding="utf-8"?><test 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><date>2007-06-15T10:30:10.5</date></test>'
doc3m = '<?xml version="1.0" encoding="utf-8"?><test 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><date>2007-06-15T10:30:10.5</date></test>'

And here is from current Mono SVN (1.2.6 same):

doc   = '<test><date>2007-06-15T10:30:10.5</date></test>'
docm  = '<?xml version="1.0" encoding="utf-8"?><test 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><date>2007-06-15T10:30:10.5+02:00</date></test>'
doc2m = '<?xml version="1.0" encoding="utf-8"?><test 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><date>2007-06-15T08:30:10.5+02:00</date></test>'
doc3m = '<?xml version="1.0" encoding="utf-8"?><test 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><date>2007-06-15T06:30:10.5+02:00</date></test>'

Note how the time "shifts" on Mono. I believe this is a bug.

Regards

Mads

-- 
Med venlig hilsen/Regards

Systemudvikler/Systemsdeveloper cand.scient.dat, Ph.d., Mads Bondo Dydensborg
Dansk BiblioteksCenter A/S, Tempovej 7-11, 2750 Ballerup, Tlf. +45 44 86 77 34




More information about the Mono-devel-list mailing list