C# 6580 should not apply when using DateTimeStyles.RoundtripKind

Version: SonarLint 7.2.0.76298 for Visual Studio

This rule (Use a format provider when parsing date and time) is raised for the following snippet, which I am using to parse dates in ISO format (e.g. returned by Javascript’s Date.toISOString()):

DateTime.TryParse(x, null, DateTimeStyles.RoundtripKind, out var v);

According the official documentation, DateTimeStyles.RoundTripKind works as follows:

The DateTimeKind field of a date is preserved when a DateTime object is converted to a string using the “o” or “r” standard format specifier, and the string is then converted back to a DateTime object.

To me this seems to imply DateTimeStyles.RoundTripKind expects a date formatted in one of the o or r formats, which means it does not care at all about the provided IFormatProvider.
If this is indeed true it would make this specific case a false positive.

Hey @m-gallesio ,

Thanks for raising this.
That is a very good point, datetimes are hard :slight_smile:

I added a ticket in our dedicated sonar-dotnet backlog for this, feel free to track it from there, as well as add any more details I might have missed.

Cheers!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.