Skip to content

Add analyzer for DateTimeOffset implicit operator with DateTime. #75400

Open

Description

@tarekgh

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.

public static implicit operator DateTimeOffset(DateTime dateTime)

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.

Activity

  1. ghost added
    untriaged
    on Sep 10, 2022
  2. added
    code-analyzer
    code-fixer
    on Sep 10, 2022
  3. KalleOlaviNiemitalo commented on Sep 11, 2022

    @KalleOlaviNiemitalo

    Will it warn about an explicit cast?

    DateTime dt = default;
    DateTimeOffset dto = (DateTimeOffset)dt;

    I would slightly prefer not warning about that. Then though, it would also have to suppress any suggestion about removing the cast.

  4. tarekgh commented on Sep 11, 2022

    @tarekgh
    MemberAuthor

    @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:

    DateTimeOffset dto = (DateTimeOffset) dt;

    will be converted to

    call valuetype [System.Runtime]System.DateTimeOffset [System.Runtime]System.DateTimeOffset::op_Implicit(valuetype [System.Runtime]System.DateTime)

    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.

  5. martincostello commented on Sep 11, 2022

    @martincostello
    Member

    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.
    
  6. ufcpp commented on Sep 12, 2022

    @ufcpp
    Contributor
    var dto = DateTimeOffset.UtcNow;
    
    // Converting from Unspecified to DTO is almost always unintentional.
    // I'd like this to be error.
    var unspecified = (DateTimeOffset)dto.DateTime;
    
    // However, Local and Utc is OK.
    // How should an analyzer suggest converting these?
    var utc = (DateTimeOffset)dto.UtcDateTime;
    var local = (DateTimeOffset)dto.LocalDateTime;
  7. added this to the Future milestone on Sep 16, 2022
  8. ghost removed
    untriaged
    on Sep 16, 2022
  9. added
    api-suggestion
    on Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.DateTimecode-analyzercode-fixer

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions