[Test]
public void Sadness()
{
var dateTime = DateTime.UtcNow;
Assert.That(dateTime, Is.EqualTo(DateTime.Parse(dateTime.ToString())));
}
Failed :
Expected: 2011-10-31 06:12:44.000
But was: 2011-10-31 06:12:44.350
I wish to know what is happening behind the scenes in ToString() etc to cause this behavior.
EDIT After seeing Jon’s Answer :
[Test]
public void NewSadness()
{
var dateTime = DateTime.UtcNow;
Assert.That(dateTime, Is.EqualTo(DateTime.Parse(dateTime.ToString("o"))));
}
Result :
Expected: 2011-10-31 12:03:04.161
But was: 2011-10-31 06:33:04.161
Same result with capital and small ‘o’ . I’m reading up the docs, but still unclear.
The default format specifier is “G” – the general-purpose format – which has limited fidelity. If you want to reproduce exactly the same thing, use the roundtrip specifier, “O”.