Skip to content

Conversation

@FstTesla
Copy link

PR Summary

Fix #25013.

When comparing DateTime instances provided as parameter -OlderThan/-NewerThan in cmdlet Test-Item, now the Kind (local, UTC or unspecified) is correctly taken into account.

Tests have also been updated, but with a caveat! ⚠️
The impacted tests acquire significance when run in a timezone with some test-dependent criteria. To ensure the test behavior behavior in a cross-platform, revertable, scoped (in-process, non-system-wide) fashion, I temporarily set a custom timezone for the duration of each test. Unfortunately, the in-process local timezone can be set only via reflection, so any improvements to this tweaky approach are welcome.

PR Context

PR Checklist

@Copilot Copilot AI review requested due to automatic review settings October 20, 2025 09:05
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 bug in the Test-Path cmdlet where DateTime comparisons with -OlderThan and -NewerThan parameters were not correctly respecting the DateTime Kind property (Local, UTC, or Unspecified). The fix ensures that all DateTime values are properly converted to UTC before comparison.

Key Changes:

  • Updated the comparison logic to convert DateTime instances to UTC while respecting their Kind property
  • Modified test setup to use UTC-based DateTime values and file timestamps
  • Added timezone manipulation in tests using reflection to ensure cross-platform test reliability

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
FileSystemProvider.cs Fixed DateTime comparison logic to convert to UTC while handling Unspecified DateTimeKind
Test-Path.Tests.ps1 Updated tests to use UTC DateTime values and added timezone manipulation for reliable cross-platform testing

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines 3381 to 3383
DateTime olderThanUtc = olderThan.Kind == DateTimeKind.Unspecified
? DateTime.SpecifyKind(olderThan, DateTimeKind.Utc)
: olderThan.ToUniversalTime();
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

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

When DateTimeKind is Unspecified, using DateTime.SpecifyKind to treat it as UTC could lead to incorrect comparisons. An Unspecified DateTime should typically be assumed to be local time and converted using DateTime.ToUniversalTime(), or the behavior should be explicitly documented. The current implementation may produce unexpected results when users pass DateTime values without specifying a Kind.

Copilot uses AI. Check for mistakes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree with Copilot:

Suggested change
DateTime olderThanUtc = olderThan.Kind == DateTimeKind.Unspecified
? DateTime.SpecifyKind(olderThan, DateTimeKind.Utc)
: olderThan.ToUniversalTime();
DateTime olderThanUtc = olderThan.ToUniversalTime();

Copy link
Author

Choose a reason for hiding this comment

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

Initially I thought that interpreting DateTimeKind.Unspecified as UTC would be more transparent; however, also in order to stick to the previous behavior, now I agree with the suggestion.
I will apply the same also below.

Comment on lines 3389 to 3391
DateTime newerThanUtc = newerThan.Kind == DateTimeKind.Unspecified
? DateTime.SpecifyKind(newerThan, DateTimeKind.Utc)
: newerThan.ToUniversalTime();
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

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

When DateTimeKind is Unspecified, using DateTime.SpecifyKind to treat it as UTC could lead to incorrect comparisons. An Unspecified DateTime should typically be assumed to be local time and converted using DateTime.ToUniversalTime(), or the behavior should be explicitly documented. The current implementation may produce unexpected results when users pass DateTime values without specifying a Kind.

Copilot uses AI. Check for mistakes.

@FstTesla
Copy link
Author

@microsoft-github-policy-service agree

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Oct 20, 2025
@iSazonov iSazonov requested a review from SteveL-MSFT October 21, 2025 08:30
@iSazonov iSazonov self-assigned this Oct 22, 2025
@iSazonov iSazonov added the CL-BreakingChange Indicates that a PR should be marked as a breaking change in the Change Log label Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-BreakingChange Indicates that a PR should be marked as a breaking change in the Change Log CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test-Path with -OlderThan/-NewerThan ignores DateTimeKind of the argument value

2 participants