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 that covers it:

[Test]
[ExpectedException(typeof(OverflowException))]
public void CreateDateWithGivenTimeHoursNotInRangeThrowOverflowException()  
{
    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…

Apparently, according to msdn, TimeSpan.Parse behaves differently in framework 4. I was very amused to see how the writer avoids the phrase Breaking change.
The docs indicates that in certain cases where in .Net framework 3.5 Parse throws an exception, in framework 4 all is good and vice versa.

Lucky we have tests…

Yossi Shmueli

Keeping it green since 1995

comments powered by Disqus