Skip to content

Conversation

@igfoo
Copy link
Contributor

@igfoo igfoo commented Sep 15, 2025

This PR corrects a typo in the UnsafeYearConstruction.qhelp file where "add/substract" is changed to "add/subtract".

Corrects "add/substract" to "add/subtract" in the UnsafeYearConstruction.qhelp
file to improve clarity.
@igfoo igfoo marked this pull request as ready for review September 15, 2025 14:45
@igfoo igfoo requested a review from a team as a code owner September 15, 2025 14:45
Copilot AI review requested due to automatic review settings September 15, 2025 14:45
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a spelling error in the C# documentation for the UnsafeYearConstruction query help file.

<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>
Copy link

Copilot AI Sep 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typo has been correctly fixed from 'substract' to 'subtract'.

Copilot uses AI. Check for mistakes.
@github-actions
Copy link
Contributor

QHelp previews:

csharp/ql/src/Likely Bugs/LeapYear/UnsafeYearConstruction.qhelp

Unsafe year argument for 'DateTime' constructor

When creating a System.DateTime object by setting the year, month, and day in the constructor by performing an arithmetic operation on a different DateTime object, there is a risk that the date you are setting is invalid.

On a leap year, such code may throw an ArgumentOutOfRangeException with a message of "Year, Month, and Day parameters describe an unrepresentable DateTime."

Recommendation

Creating a System.DateTime object based on a different System.DateTime object, use the appropriate methods to manipulate the date instead of arithmetic.

Example

In this example, we are incrementing/decrementing the current date by one year when creating a new System.DateTime object. This may work most of the time, but on any given February 29th, the resulting value will be invalid.

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 AddYears method on it.

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

@igfoo igfoo merged commit b797df6 into main Sep 15, 2025
19 checks passed
@igfoo igfoo deleted the igfoo/fix-typo-substract branch September 15, 2025 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants