WiseTechGlobal / WTG.Analyzers

Analyzers from WiseTech Global to enforce our styles, behaviours, and prevent common mistakes.
Other
15 stars 3 forks source link

Analyzer to warn when comparing DateTime with DateTimeOffset #195

Open yaakov-h opened 1 year ago

yaakov-h commented 1 year ago

In Australia/Sydney UTC+11:

using System;
var dt = new DateTime(DateTime.UtcNow.AddMinutes(10).Ticks, DateTimeKind.Unspecified);

dt > DateTime.Now // false
dt > DateTime.UtcNow // true
dt > DateTimeOffset.Now // false
dt > DateTimeOffset.UtcNow // also false!!!

We can get "Unspecified" DateTimes from a variety of places including deserialized data such as SQL/EntityFramework.

Comparing an Unspecified DateTime with a DateTimeOffset is a potential source of bugs as it trigger a conversion to local time in both .NET Framework and in modern .NET.

Tracked by WI00562111.