DateTime serialization bug

Oct 21, 2012 at 12:42 PM

Hi, following code demonstrates the bug:

namespace ConsoleApplication1
{
	using System;

	public class Program
	{
		private static void Main(string[] args)
		{
			var time = new DateTime(2012, 12, 31);
			var serializer = new YAXLib.YAXSerializer(typeof (DateTime));
			serializer.SerializeToFile(time, "time.xml");
			var newTime = (DateTime)serializer.DeserializeFromFile("time.xml");

			Console.WriteLine(time);
			Console.WriteLine(newTime);

			Console.ReadLine();
		}
	}
}

Set your region to for example Finland (Finnish) to see the bug.

Here's my output:

31.12.2012 0:00:00
1.1.0001 0:00:00

Why not just use DateTime.ToString("o") format (see http://msdn.microsoft.com/en-us/library/az4se3k1.aspx#Roundtrip)? It would preserve much more information than the current format and it would not have these culture issues.

Oct 21, 2012 at 12:52 PM

I just realized I could use [YAXFormat("o")] attribute which fixes the problem.