Skip to content

Analyzer to warn when comparing DateTime with DateTimeOffset #195

@yaakov-h

Description

@yaakov-h

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions