From ca0178a80219a2ca12f323a15501fda76340ba16 Mon Sep 17 00:00:00 2001 From: Bryan Berns Date: Tue, 12 Jul 2022 02:09:19 -0400 Subject: [PATCH] Add Milliseconds Parameter to New-TimeSpan (#17621) --- .../commands/utility/NewTimeSpanCommand.cs | 8 ++++- .../New-TimeSpan.Tests.ps1 | 30 ++++++++++--------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTimeSpanCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTimeSpanCommand.cs index 53c3925076..a5d784da9b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTimeSpanCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTimeSpanCommand.cs @@ -88,6 +88,12 @@ namespace Microsoft.PowerShell.Commands [Parameter(ParameterSetName = "Time")] public int Seconds { get; set; } + /// + /// Allows the user to override the millisecond. + /// + [Parameter(ParameterSetName = "Time")] + public int Milliseconds { get; set; } + #endregion #region methods @@ -119,7 +125,7 @@ namespace Microsoft.PowerShell.Commands break; case "Time": - result = new TimeSpan(Days, Hours, Minutes, Seconds); + result = new TimeSpan(Days, Hours, Minutes, Seconds, Milliseconds); break; default: diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/New-TimeSpan.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/New-TimeSpan.Tests.ps1 index ad8cb8a612..5dcc815b08 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/New-TimeSpan.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/New-TimeSpan.Tests.ps1 @@ -3,12 +3,13 @@ Describe "New-TimeSpan DRT Unit Tests" -Tags "CI" { It "Should works proper with new-timespan"{ - $results = New-TimeSpan -Days 10 -Hours 10 -Minutes 10 -Seconds 10 + $results = New-TimeSpan -Days 10 -Hours 10 -Minutes 10 -Seconds 10 -Milliseconds 10 $results | Should -BeOfType System.Timespan $results.Days | Should -Be 10 $results.Hours | Should -Be 10 $results.Minutes | Should -Be 10 $results.Seconds | Should -Be 10 + $results.Milliseconds | Should -Be 10 } } @@ -20,29 +21,30 @@ Describe "New-TimeSpan" -Tags "CI" { } Context "Core Functionality Tests" { - New-Variable -Name testObject -Value $(New-TimeSpan -Days 2 -Hours 23 -Minutes 4 -Seconds 3) -Force + New-Variable -Name testObject -Value $(New-TimeSpan -Days 2 -Hours 23 -Minutes 4 -Seconds 3 -Milliseconds 2) -Force $expectedOutput = @{ "Days" = "2"; "Hours" = "23"; "Minutes" = "4"; "Seconds" = "3"; - "Milliseconds" = "0"; - "Ticks" = "2558430000000"; - "TotalDays" = "2.96114583333333"; - "TotalHours" = "71.0675"; - "TotalMinutes" = "4264.05"; - "TotalSeconds" = "255843"; - "TotalMilliseconds" = "255843000" + "Milliseconds" = "2"; + "Ticks" = "2558430020000"; + "TotalDays" = "2.96114585648148"; + "TotalHours" = "71.0675005555556"; + "TotalMinutes" = "4264.05003333333"; + "TotalSeconds" = "255843.002"; + "TotalMilliseconds" = "255843002" } $TEN_MILLION = 10000000 It "Should have expected values for time properties set during creation" { - $testObject.Days | Should -Be $expectedOutput["Days"] - $testObject.Hours | Should -Be $expectedOutput["Hours"] - $testObject.Minutes | Should -Be $expectedOutput["Minutes"] - $testObject.Seconds | Should -Be $expectedOutput["Seconds"] - $testObject.Ticks | Should -Be $expectedOutput["Ticks"] + $testObject.Days | Should -Be $expectedOutput["Days"] + $testObject.Hours | Should -Be $expectedOutput["Hours"] + $testObject.Minutes | Should -Be $expectedOutput["Minutes"] + $testObject.Seconds | Should -Be $expectedOutput["Seconds"] + $testObject.Milliseconds | Should -Be $expectedOutput["Milliseconds"] + $testObject.Ticks | Should -Be $expectedOutput["Ticks"] } }