Conversation
Corrects "add/substract" to "add/subtract" in the UnsafeYearConstruction.qhelp file to improve clarity.
| <p>In this example, we are incrementing/decrementing the current date by one year when creating a new <code>System.DateTime</code> object. This may work most of the time, but on any given February 29th, the resulting value will be invalid.</p> | ||
| <sample src="UnsafeYearConstructionBad.cs" /> | ||
| <p>To fix this bug, we add/substract years to the current date by calling <code>AddYears</code> method on it.</p> | ||
| <p>To fix this bug, we add/subtract years to the current date by calling <code>AddYears</code> method on it.</p> |
There was a problem hiding this comment.
The typo has been correctly fixed from 'substract' to 'subtract'.
|
QHelp previews: csharp/ql/src/Likely Bugs/LeapYear/UnsafeYearConstruction.qhelpUnsafe year argument for 'DateTime' constructorWhen creating a On a leap year, such code may throw an RecommendationCreating a ExampleIn this example, we are incrementing/decrementing the current date by one year when creating a new using System;
public class UnsafeYearConstructionBad
{
public UnsafeYearConstructionBad()
{
DateTime Start;
DateTime End;
var now = DateTime.UtcNow;
// the base-date +/- n years may not be a valid date.
Start = new DateTime(now.Year - 1, now.Month, now.Day, 0, 0, 0, DateTimeKind.Utc);
End = new DateTime(now.Year + 1, now.Month, now.Day, 0, 0, 1, DateTimeKind.Utc);
}
}To fix this bug, we add/subtract years to the current date by calling using System;
public class UnsafeYearConstructionGood
{
public UnsafeYearConstructionGood()
{
DateTime Start;
DateTime End;
var now = DateTime.UtcNow;
Start = now.AddYears(-1).Date;
End = now.AddYears(-1).Date.AddSeconds(1);
}
}References |
This PR corrects a typo in the UnsafeYearConstruction.qhelp file where "add/substract" is changed to "add/subtract".