2020-12-31

Is there a bug in the DateTime.Parse method when UTC is used with time zone offsets?

The Microsoft documentation states that, among other string representations of DateTime structures, DateTime.Parse(string) conforms to ISO 8601 and accepts Coordinated Universal Time (UTC) formats for time zone offsets.

When I run the following unit test cases, DateTime.Parse(string) and DateTime.TryParse(string, out DateTime) accepts an additional character at the end of the source string. This seems to be a bug. When more than one additional character is appended, the method correctly fails to parse the string.

[Theory]
[InlineData("2020-5-7T09:37:00.0000000-07:00")]
[InlineData("2020-5-7T09:37:00.0000000-07:00x")]
[InlineData("2020-5-7T09:37:00.0000000-07:00xx")]
[InlineData("2020-5-7T09:37:00.0000000Z")]
[InlineData("2020-5-7T09:37:00.0000000Zx")]
public void TestParse(string source)
{
    DateTime dt = DateTime.Parse(source);
    Assert.True(dt != null);
    bool b = DateTime.TryParse(source, out dt);
    Assert.True(b);
}

This unit test case was written to simplify my code and illustrate the behavior I am seeing (I recognize that expected failures should be handled differently).

Tests 1 and 2 pass and the 3rd test (with the "xx" suffix) fails. It seems to me that the second test (with the "x" suffix) should fail.

When no time zone designator is provided, test 4 passes and test 5 fails. This seems to be correct behavior.

I'm wondering if anyone has encountered this, and if so, is there general agreement that this is a bug?



from Recent Questions - Stack Overflow https://ift.tt/3o3OT8X
https://ift.tt/eA8V8J

No comments:

Post a Comment