Skip to content

Fix ConvertTo-Json DateTime timezone test#27524

Open
KirtiRamchandani wants to merge 1 commit into
PowerShell:masterfrom
KirtiRamchandani:fix/convertto-json-datetime-timezone-test
Open

Fix ConvertTo-Json DateTime timezone test#27524
KirtiRamchandani wants to merge 1 commit into
PowerShell:masterfrom
KirtiRamchandani:fix/convertto-json-datetime-timezone-test

Conversation

@KirtiRamchandani
Copy link
Copy Markdown

PR Summary

Make the ConvertTo-Json DateTime ETS test independent of the machine's local timezone.

PR Context

The test parsed 2021-06-24T15:54:06.796999-07:00 into a local DateTime, then expected the compressed JSON to start with 2021-06-24. On machines at UTC+1 or later, that same instant serializes as the next local date, so the assertion fails even though ConvertTo-Json is behaving correctly.

This normalizes the fixture through DateTimeOffset.Parse(...).UtcDateTime and checks the exact UTC JSON string. The roundtrip assertion remains in place, so the test still verifies that DateTime is serialized as a scalar value rather than with ETS properties.

Fixes #27500

PR Checklist

Tests

  • Reproduced failure before the fix with Start-PSPester -Path ./test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 -UseNuGetOrg -SkipTestToolBuild -ThrowOnFailure
  • Start-PSPester -Path ./test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 -UseNuGetOrg -SkipTestToolBuild -ThrowOnFailure (222 passed, 1 pending)
  • git diff --check

Copilot AI review requested due to automatic review settings May 25, 2026 01:15
@KirtiRamchandani KirtiRamchandani requested a review from a team as a code owner May 25, 2026 01:15
Copy link
Copy Markdown
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

Note

Copilot was unable to run its full agentic suite in this review.

Updates a ConvertTo-Json test to make the expected JSON output deterministic by normalizing a DateTime value to UTC and asserting an exact ISO-8601 string.

Changes:

  • Parse the input timestamp as DateTimeOffset and convert to UtcDateTime.
  • Replace a wildcard string assertion with an exact -BeExactly assertion for the JSON output.

It 'Should not serialize ETS properties added to DateTime' {
$date = "2021-06-24T15:54:06.796999-07:00"
$d = [DateTime]::Parse($date)
$d = [DateTimeOffset]::Parse($date).UtcDateTime

# need to use wildcard here due to some systems may be configured with different culture setting showing time in different format
$d | ConvertTo-Json -Compress | Should -BeLike '"2021-06-24T*'
$d | ConvertTo-Json -Compress | Should -BeExactly '"2021-06-24T22:54:06.796999Z"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ConvertTo-Json.Tests.ps1 has a broken test depending on timezones

2 participants