Thursday, October 14, 2010

TimeSpan.Parse breaking change in .Net Framework 4

This week my team upgraded our solutions to Visual Studio 2010 and .Net framework 4. When we ran the tests of one our projects, one of our tests failed with a weird reason. The code is simple:
public DateTime CreateDateWithGivenTime(string time)
{

var t = TimeSpan.Parse(time);
...
}
The test was:
[Test]
[ExpectedException(typeof(OverflowException))]
public void CreateDateWithGivenTime_HoursNotInRange_ThrowOverflowException()
{

CreateDateWithGivenTime("77:11:00");
}

Testing on a branch before the conversion – the test passed.
Debugging the code in both cases showed different behavior of the framework:
Framework 3.5: Throws exception
Framework 4: Code recovers and parses the string as if “77” is days, “11” is hours etc…

Read more: YsA.net