MA0113: Only report DateTime constructors with the Utc kind - #1458
Merged
Merged
Conversation
DateTime.UnixEpoch has the DateTimeKind.Utc kind, while the constructors without a DateTimeKind argument create a DateTimeKind.Unspecified value. Replacing them with DateTime.UnixEpoch changed the Kind of the value, which affects time zone conversions and serialization. The rule now only reports the constructors that explicitly pass DateTimeKind.Utc, and also handles the millisecond and microsecond overloads, as MA0114 does for DateTimeOffset.
This was referenced Sep 11, 2026
Closed
This was referenced Sep 17, 2026
Merged
Open
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
MA0113 reported
DateTimeconstructors that don't take aDateTimeKind, and its fix replaced them withDateTime.UnixEpoch:Both compile, but the first returns
DateTimeKind.Unspecifiedand the second returnsDateTimeKind.Utc. The ticks are equal, but the values aren't interchangeable. The kind matters to code that inspectsKind, converts between time zones, or serializes the kind.Changes
new DateTime(ticks),new DateTime(1970, 1, 1)andnew DateTime(1970, 1, 1, 0, 0, 0). The rule now only reports constructors that explicitly passDateTimeKind.Utc. It also recognizes the millisecond and microsecond overloads withDateTimeKind.Utc, which MA0114 already handles forDateTimeOffset.LocalandUnspecifiedkinds, non-zero milliseconds and microseconds, and theCalendaroverload. The old-framework test now uses a UTC constructor, so it still checks that nothing is reported whenDateTime.UnixEpochdoesn't exist.MA0113.mdno longer recommends replacingnew DateTime(1970, 1, 1). It explains why constructors without an explicitDateTimeKind.Utcaren't reported.Notes for reviewers
new DateTime(1970, 1, 1)won't get a diagnostic anymore. That's intended, because the fix would have changed behavior.DateTimeOffset) is unchanged.DateTimeOffsethas no kind, and every case it reports requires a zero offset.Testing
UseDateTimeUnixEpochAnalyzerTestspasses on Roslyn 4.8, 4.14, 5.0, 5.6 and 5.9 (45 tests each).dotnet run --project src/DocumentationGeneratorexits 0 and generates no further changes.