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

Implement S6562: Always set the DateTimeKind when creating a new DateTime object #7527

Merged
merged 12 commits into from
Jul 4, 2023
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"issues": [
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\Ember-MM\EmberAPI\clsAPICommon.vb",
"region": {
"startLine": 711,
"startColumn": 34,
"endLine": 711,
"endColumn": 70
}
}
},
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\Ember-MM\EmberAPI\clsAPICommon.vb",
"region": {
"startLine": 716,
"startColumn": 34,
"endLine": 716,
"endColumn": 70
}
}
},
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\Ember-MM\EmberAPI\clsAPIScanner.vb",
"region": {
"startLine": 32,
"startColumn": 30,
"endLine": 32,
"endColumn": 42
}
}
}
]
}
17 changes: 17 additions & 0 deletions analyzers/its/expected/akka.net/Akka--netstandard2.0-S6562.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"issues": [
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\akka.net\src\core\Akka\Util\Extensions\DateTimeExtensions.cs",
"region": {
"startLine": 22,
"startColumn": 38,
"endLine": 22,
"endColumn": 62
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"issues": [
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\akka.net\src\contrib\cluster\Akka.DistributedData\Serialization\ReplicatorMessageSerializer.cs",
"region": {
"startLine": 578,
"startColumn": 67,
"endLine": 578,
"endColumn": 99
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
{
"issues": [
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\akka.net\src\core\Akka.MultiNodeTestRunner.Shared\Persistence\TimelineItemFactory.cs",
"region": {
"startLine": 38,
"startColumn": 73,
"endLine": 38,
"endColumn": 101
}
}
},
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\akka.net\src\core\Akka.MultiNodeTestRunner.Shared\Persistence\TimelineItemFactory.cs",
"region": {
"startLine": 48,
"startColumn": 84,
"endLine": 48,
"endColumn": 112
}
}
},
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\akka.net\src\core\Akka.MultiNodeTestRunner.Shared\Persistence\VisualizerRuntimeTemplate.Tree.cs",
"region": {
"startLine": 100,
"startColumn": 38,
"endLine": 100,
"endColumn": 71
}
}
},
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\akka.net\src\core\Akka.MultiNodeTestRunner.Shared\Persistence\VisualizerRuntimeTemplate.Tree.cs",
"region": {
"startLine": 101,
"startColumn": 38,
"endLine": 101,
"endColumn": 70
}
}
},
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\akka.net\src\core\Akka.MultiNodeTestRunner.Shared\Reporting\TestRunTree.cs",
"region": {
"startLine": 61,
"startColumn": 45,
"endLine": 61,
"endColumn": 72
}
}
},
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\akka.net\src\core\Akka.MultiNodeTestRunner.Shared\Reporting\TestRunTree.cs",
"region": {
"startLine": 61,
"startColumn": 94,
"endLine": 61,
"endColumn": 117
}
}
},
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\akka.net\src\core\Akka.MultiNodeTestRunner.Shared\Reporting\TestRunTree.cs",
"region": {
"startLine": 196,
"startColumn": 45,
"endLine": 196,
"endColumn": 72
}
}
},
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\akka.net\src\core\Akka.MultiNodeTestRunner.Shared\Reporting\TestRunTree.cs",
"region": {
"startLine": 196,
"startColumn": 94,
"endLine": 196,
"endColumn": 117
}
}
},
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\akka.net\src\core\Akka.MultiNodeTestRunner.Shared\Reporting\TestRunTree.cs",
"region": {
"startLine": 350,
"startColumn": 45,
"endLine": 350,
"endColumn": 72
}
}
},
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\akka.net\src\core\Akka.MultiNodeTestRunner.Shared\Reporting\TestRunTree.cs",
"region": {
"startLine": 350,
"startColumn": 94,
"endLine": 350,
"endColumn": 117
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"issues": [
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\akka.net\src\core\Akka.Persistence\Snapshot\LocalSnapshotStore.cs",
"region": {
"startLine": 295,
"startColumn": 66,
"endLine": 295,
"endColumn": 85
}
}
},
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\akka.net\src\core\Akka.Persistence\Snapshot\MemorySnapshotStore.cs",
"region": {
"startLine": 102,
"startColumn": 101,
"endLine": 102,
"endColumn": 130
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"issues": [
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\akka.net\src\contrib\persistence\Akka.Persistence.Sqlite\Snapshot\SqliteSnapshotStore.cs",
"region": {
"startLine": 88,
"startColumn": 29,
"endLine": 88,
"endColumn": 61
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"issues": [
{
"id": "S6562",
"message": "Provide the "DateTimeKind" when creating this object.",
"location": {
"uri": "sources\akka.net\src\core\Akka.Persistence.TCK\Snapshot\SnapshotStoreSpec.cs",
"region": {
"startLine": 119,
"startColumn": 99,
"endLine": 119,
"endColumn": 119
}
}
}
]
}
39 changes: 39 additions & 0 deletions analyzers/rspec/cs/S6562.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<h2>Why is this an issue?</h2>
<p>Not knowing the <code>Kind</code> of the <code>DateTime</code> object that an application is using can lead to misunderstandings when displaying or
comparing them. Explicitly setting the <code>Kind</code> property helps the application to stay consistent, and its maintainers understand what kind
of date is being managed. To achieve this, when instantiating a new <code>DateTime</code> object you should always use a constructor overload that
allows you to define the <code>Kind</code> property.</p>
<h3>What is the potential impact?</h3>
<p>Creating the <code>DateTime</code> object without specifying the property <code>Kind</code> will set it to the default value of
<code>DateTimeKind.Unspecified</code>. In this case, calling the method <code>ToUniversalTime</code> will assume that <code>Kind</code> is
<code>DateTimeKind.Local</code> and calling the method <code>ToLocalTime</code> will assume that it’s <code>DateTimeKind.Utc</code>. As a result, you
might have mismatched <code>DateTime</code> objects in your application.</p>
<h2>How to fix it</h2>
<p>To fix this issue use a constructor overload that allows specifying the <code>DateTimeKind</code> when creating the <code>DateTime</code>
object.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
void CreateNewTime()
{
var birthDate = new DateTime(1994, 7, 5, 16, 23, 42);
}
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
void CreateNewTime()
{
var birthDate = new DateTime(1994, 7, 5, 16, 23, 42, DateTimeKind.Utc);
}
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.datetimekind">DateTimeKind documentation</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.datetime.-ctor">DateTime documentation</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/standard/base-types/how-to-round-trip-date-and-time-values">How to round trip date and time
values</a> </li>
<li> <a
href="https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/member-overloading">https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/member-overloading</a> [Member overloading] </li>
</ul>

18 changes: 18 additions & 0 deletions analyzers/rspec/cs/S6562.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"title": "Always set the \"DateTimeKind\" when creating new \"DateTime\" instances",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"localisation",
"pitfall"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6562",
"sqKey": "S6562",
"scope": "All",
"quickfix": "infeasible"
}
1 change: 1 addition & 0 deletions analyzers/rspec/cs/Sonar_way_profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@
"S6424",
"S6444",
"S6561",
"S6562",
"S6575",
"S6588",
"S6602",
Expand Down
37 changes: 37 additions & 0 deletions analyzers/rspec/vbnet/S6562.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<h2>Why is this an issue?</h2>
<p>Not knowing the <code>Kind</code> of the <code>DateTime</code> object that an application is using can lead to misunderstandings when displaying or
comparing them. Explicitly setting the <code>Kind</code> property helps the application to stay consistent, and its maintainers understand what kind
of date is being managed. To achieve this, when instantiating a new <code>DateTime</code> object you should always use a constructor overload that
allows you to define the <code>Kind</code> property.</p>
<h3>What is the potential impact?</h3>
<p>Creating the <code>DateTime</code> object without specifying the property <code>Kind</code> will set it to the default value of
<code>DateTimeKind.Unspecified</code>. In this case, calling the method <code>ToUniversalTime</code> will assume that <code>Kind</code> is
<code>DateTimeKind.Local</code> and calling the method <code>ToLocalTime</code> will assume that it’s <code>DateTimeKind.Utc</code>. As a result, you
might have mismatched <code>DateTime</code> objects in your application.</p>
<h2>How to fix it</h2>
<p>To fix this issue use a constructor overload that allows specifying the <code>DateTimeKind</code> when creating the <code>DateTime</code>
object.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
Private Sub CreateNewTime()
Dim birthDate = New DateTime(1994, 7, 5, 16, 23, 42)
End Sub
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
Private Sub CreateNewTime()
Dim birthDate = New DateTime(1994, 7, 5, 16, 23, 42, DateTimeKind.Utc)
End Sub
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.datetimekind">DateTimeKind documentation</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.datetime.-ctor">DateTime documentation</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/standard/base-types/how-to-round-trip-date-and-time-values">How to round trip date and time
values</a> </li>
<li> <a
href="https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/member-overloading">https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/member-overloading</a> [Member overloading] </li>
</ul>

Loading