-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Roslyn analyzers for bug-prone usage of DateTime/DateTimeOffset #79105
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Yes, especially for one surprising reason - at least one culture uses a
You should be using DateTimeOffset.TryParse(expiryString, DateTimeFormatInfo.InvariantInfo, out var expiry) ... which is explicitly using a culture. |
Well, I'm glad my attempt at a fix is wrong if it makes the case for an analyzer stronger :) |
Actually, if you previously know the format of the date string you should use
|
I don't think you should recommend invariant culture--you just need to recommend using an API where the culture is explicitly specified, similar to how string comparison API analyzers recommend you specify the culture explicitly. |
I recently ran into two separate bugs related to time handling in our code and thought roslyn analyzers might be able to assist.
The two instances were:
DateTimeOffset.Parse("2020-01-02").ToString(...)
and
if (DateTimeOffset.TryParse(expiryString, out var expiry) && now < expiry) // expiry is a utc YYYY-MM-DD date
in both of these usages, I should have used something like
DateTimeOffset.TryParse(expiryString, null, System.Globalization.DateTimeStyles.AssumeUniversal, out var expiry)
I see this as similar to requiring explicit usage of culture objects in string comparisons. Is there an analyzer that could recommend the version of the function with the explicit default behavior request like we have for string comparison functions?
Semi-related #43956
The text was updated successfully, but these errors were encountered: