Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/System.Management.Automation/engine/hostifaces/History.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Host;
Expand Down Expand Up @@ -1449,14 +1450,14 @@ void ProcessRecord()

// Read StartExecutionTime property
object temp = GetPropertyValue(mshObject, "StartExecutionTime");
if (temp == null || !LanguagePrimitives.TryConvertTo<DateTime>(temp, out DateTime startExecutionTime))
if (temp == null || !LanguagePrimitives.TryConvertTo<DateTime>(temp, CultureInfo.CurrentCulture, out DateTime startExecutionTime))
{
break;
}

// Read EndExecutionTime property
temp = GetPropertyValue(mshObject, "EndExecutionTime");
if (temp == null || !LanguagePrimitives.TryConvertTo<DateTime>(temp, out DateTime endExecutionTime))
if (temp == null || !LanguagePrimitives.TryConvertTo<DateTime>(temp, CultureInfo.CurrentCulture, out DateTime endExecutionTime))
{
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,58 @@ Describe "History cmdlet test cases" -Tags "CI" {
}
}

Context 'Conversions and Culture tests' {

BeforeAll {
$cultureTestCases = @(
@{
Culture = 'en-us'
StartTime = '08/18/2021 16:43:50'
EndTime = '08/18/2021 16:44:50'
}
@{
Culture = 'en-au'
StartTime = '18/08/2021 16:43:50'
EndTime = '18/08/2021 16:44:50'
}
)

$oldCulture = [cultureinfo]::CurrentCulture
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here and below:

Suggested change
$oldCulture = [cultureinfo]::CurrentCulture
$oldCulture = [CultureInfo]::CurrentCulture

Copy link
Collaborator

Choose a reason for hiding this comment

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

We seem to have a type accelerator for this, so I'd argue that [cultureinfo] is preferred, like [bool] or [pscustomobject]

}

AfterEach {
[cultureinfo]::CurrentCulture = $oldCulture
}

It "respects current culture settings when handling datetime conversions" -TestCases $cultureTestCases {
param($Culture, $StartTime, $EndTime)

[cultureinfo]::CurrentCulture = [cultureinfo]::GetCultureInfo($Culture)

$history = [PSCustomObject] @{
CommandLine = "test-command"
ExecutionStatus = [Management.Automation.Runspaces.PipelineState]::Completed
StartExecutionTime = $StartTime
EndExecutionTime = $EndTime
}

{ $history | Add-History -ErrorAction Stop } | Should -Not -Throw -Because 'the datetime should be converted according to the current culture'
}

It "throws an error when asked to convert a date format that doesn't match the current culture" {
[cultureinfo]::CurrentCulture = [cultureinfo]::GetCultureInfo('en-au')
$history = [PSCustomObject] @{
CommandLine = "test-command"
ExecutionStatus = [Management.Automation.Runspaces.PipelineState]::Completed
StartExecutionTime = '08/18/2021 16:43:50'
EndExecutionTime = '08/18/2021 16:44:50'
}

$errorMessage = 'Cannot add history because the input object has a format that is not valid.'
{ $history | Add-History -ErrorAction Stop } | Should -Throw -ExpectedMessage $errorMessage
}
}

It "Tests Invoke-History on a cmdlet that generates output on all streams" {
$streamSpammer = '
function StreamSpammer
Expand Down