Skip to content
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

Analyzer to warn when comparing DateTime with DateTimeOffset #195

Open
yaakov-h opened this issue Dec 22, 2022 · 0 comments
Open

Analyzer to warn when comparing DateTime with DateTimeOffset #195

yaakov-h opened this issue Dec 22, 2022 · 0 comments

Comments

@yaakov-h
Copy link
Member

yaakov-h commented Dec 22, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant