You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add analyzer for DateTimeOffset implicit operator with DateTime. #75400
The DateTimeOffset implicit operator internally convert the input DateTime to DateTimeOffset. This conversion will depend on the DateTime.Kind value. If the value is Unspecified or Local, will automatically calculate the offset between the current local time zone and UTC and use this offset for creating DateTimeOffset. If the Kind value is Utc, will use 0 offset to create the DateTimeOffset object.
Most users unaware about this and not paying much attention when this implicit operator get used and can get a surprised result. The issue can be much worse if this operator used with the time zone APIs as described in #32954
The request here is to have analyzer detect the usage of this implicit operator and produce a suggestion to the user.
Something like:
Using DateTimeOffset(DateTime dateTime) implicit operator can produce a surprising result depending on the Kind value stored in the input DateTime. Consider using the explicit DateTimeOffset constructor that takes the dateTime.Ticks and the desired offset.
Or any better wording.
Reacted by Dan Moseley, Nobuyuki Iwanaga, Huo Yaoyuan, Martin Costello, Matt Johnson-Pint, trympet, Avi Levin, Frulfump, svenesters, Rushui Guan and 13 more
Activity
ghost added
untriagedNew issue has not been triaged by the area owner
@KalleOlaviNiemitalo that is a good question. The current proposal is for implicit casting so does not include explicit casting.
I would say explicit casting is explicit user intention, but I believe it will suffer from the same thing if the user is not aware about the expected behavior. So, it makes sense the analyzer will include this case too. But I want to hear more feedback if this can be an issue.
Also, the compiler will use the implicit casting to do the explicit one:
Note the analyzer suggestion is not going to break any build or produce any warning. It is just info which users can even ignore without suppressing it.
I think the implicit version is the most important one to focus on, as it's often done by accident and can lead to bugs that can take up to six months to become apparent.
This has been the source of a number of major bugs in applications I've worked on over the last few years where behaviour around daylight savings transitions is important and implicit conversions have caused values to end up being compared incorrectly leading to incorrect behaviour.
To try and prevent this we've adopted the Microsoft.CodeAnalysis.BannedApiAnalyzers to specifically detect any accidental usage of it with a warning via the following entry in BannedSymbols.txt:
M:System.DateTimeOffset.op_Implicit(System.DateTime); Do not implicitly cast DateTime to DateTimeOffset.
Reacted by Dan Moseley, YohanS, Avi Levin, andrewimcclement, Will Rogers, Yutaka Murata and Frulfump
vardto=DateTimeOffset.UtcNow;// Converting from Unspecified to DTO is almost always unintentional.// I'd like this to be error.varunspecified=(DateTimeOffset)dto.DateTime;// However, Local and Utc is OK.// How should an analyzer suggest converting these?varutc=(DateTimeOffset)dto.UtcDateTime;varlocal=(DateTimeOffset)dto.LocalDateTime;
Activity
KalleOlaviNiemitalo commentedon Sep 11, 2022
Will it warn about an explicit cast?
I would slightly prefer not warning about that. Then though, it would also have to suppress any suggestion about removing the cast.
tarekgh commentedon Sep 11, 2022
@KalleOlaviNiemitalo that is a good question. The current proposal is for implicit casting so does not include explicit casting.
I would say explicit casting is explicit user intention, but I believe it will suffer from the same thing if the user is not aware about the expected behavior. So, it makes sense the analyzer will include this case too. But I want to hear more feedback if this can be an issue.
Also, the compiler will use the implicit casting to do the explicit one:
will be converted to
Note the analyzer
suggestionis not going to break any build or produce any warning. It is just info which users can even ignore without suppressing it.martincostello commentedon Sep 11, 2022
I think the implicit version is the most important one to focus on, as it's often done by accident and can lead to bugs that can take up to six months to become apparent.
This has been the source of a number of major bugs in applications I've worked on over the last few years where behaviour around daylight savings transitions is important and implicit conversions have caused values to end up being compared incorrectly leading to incorrect behaviour.
To try and prevent this we've adopted the Microsoft.CodeAnalysis.BannedApiAnalyzers to specifically detect any accidental usage of it with a warning via the following entry in
BannedSymbols.txt:ufcpp commentedon Sep 12, 2022