From 8e6e0051ceec7e0ecfe13495f71b994250e13310 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 11 Jul 2025 11:09:37 +0000 Subject: [PATCH 01/12] Initial plan From 33b881f103d20b540ec190381da0091dba321736 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 11 Jul 2025 11:19:09 +0000 Subject: [PATCH 02/12] Implement UseSymbols parameter for Format-TimeSpan with updated abbreviations and symbols Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com> --- src/functions/private/Format-UnitValue.ps1 | 39 +++++++++++---- src/functions/public/Format-TimeSpan.ps1 | 35 ++++++++++---- src/variables/private/UnitMap.ps1 | 37 ++++++++++---- tests/PSModuleTest.Tests.ps1 | 56 ++++++++++++++++++++-- 4 files changed, 135 insertions(+), 32 deletions(-) diff --git a/src/functions/private/Format-UnitValue.ps1 b/src/functions/private/Format-UnitValue.ps1 index 8144c57..c46e620 100644 --- a/src/functions/private/Format-UnitValue.ps1 +++ b/src/functions/private/Format-UnitValue.ps1 @@ -6,40 +6,51 @@ .DESCRIPTION This function takes an integer value and a unit and returns a formatted string. If the -FullNames switch is specified, the function uses the singular or plural full unit name. + If the -UseSymbols switch is specified, the function uses symbols instead of abbreviations. Otherwise, it returns the value with the unit abbreviation. .EXAMPLE - Format-UnitValue -Value 5 -Unit 'meter' + Format-UnitValue -Value 5 -Unit 'Hours' Output: ```powershell - 5m + 5hr ``` Returns the formatted value with its abbreviation. .EXAMPLE - Format-UnitValue -Value 1 -Unit 'meter' -FullNames + Format-UnitValue -Value 5 -Unit 'Hours' -UseSymbols Output: ```powershell - 1 meter + 5h + ``` + + Returns the formatted value with its symbol. + + .EXAMPLE + Format-UnitValue -Value 1 -Unit 'Hours' -FullNames + + Output: + ```powershell + 1 hour ``` Returns the formatted value with the full singular unit name. .EXAMPLE - Format-UnitValue -Value 2 -Unit 'meter' -FullNames + Format-UnitValue -Value 2 -Unit 'Hours' -FullNames Output: ```powershell - 2 meters + 2 hours ``` Returns the formatted value with the full plural unit name. .OUTPUTS - string. A formatted string combining the value and its corresponding unit abbreviation or full name. + string. A formatted string combining the value and its corresponding unit abbreviation, symbol, or full name. .LINK https://psmodule.io/Format/Functions/Format-UnitValue/ @@ -55,9 +66,13 @@ [Parameter(Mandatory)] [string] $Unit, - # Switch to use full unit names instead of abbreviations. + # Switch to use full unit names instead of abbreviations or symbols. [Parameter()] - [switch] $FullNames + [switch] $FullNames, + + # Switch to use symbols instead of abbreviations. + [Parameter()] + [switch] $UseSymbols ) if ($FullNames) { @@ -66,5 +81,9 @@ return "$Value $unitName" } - "$Value$($script:UnitMap[$Unit].Abbreviation)" + if ($UseSymbols) { + "$Value$($script:UnitMap[$Unit].Symbol)" + } else { + "$Value$($script:UnitMap[$Unit].Abbreviation)" + } } diff --git a/src/functions/public/Format-TimeSpan.ps1 b/src/functions/public/Format-TimeSpan.ps1 index 92975e4..72f2ed9 100644 --- a/src/functions/public/Format-TimeSpan.ps1 +++ b/src/functions/public/Format-TimeSpan.ps1 @@ -5,19 +5,29 @@ .DESCRIPTION This function converts a TimeSpan object into a formatted string based on a chosen unit or precision. - It allows specifying a base unit, the number of precision levels, and whether to use full unit names. - If the TimeSpan is negative, it is prefixed with a minus sign. + It allows specifying a base unit, the number of precision levels, and whether to use full unit names, + symbols, or abbreviations. If the TimeSpan is negative, it is prefixed with a minus sign. .EXAMPLE New-TimeSpan -Minutes 90 | Format-TimeSpan Output: ```powershell - 2h + 2hr ``` Formats the given TimeSpan into a human-readable format using the most significant unit. + .EXAMPLE + New-TimeSpan -Minutes 90 | Format-TimeSpan -UseSymbols + + Output: + ```powershell + 2h + ``` + + Formats the given TimeSpan using symbols instead of abbreviations. + .EXAMPLE [TimeSpan]::FromSeconds(3661) | Format-TimeSpan -Precision 2 -FullNames @@ -61,9 +71,13 @@ [Parameter()] [string] $BaseUnit, - # If specified, outputs full unit names instead of abbreviations. + # If specified, outputs full unit names instead of abbreviations or symbols. [Parameter()] - [switch] $FullNames + [switch] $FullNames, + + # If specified, uses symbols instead of abbreviations for unit formatting. + [Parameter()] + [switch] $UseSymbols ) process { @@ -74,7 +88,10 @@ $originalTicks = $TimeSpan.Ticks # Ordered list of units from most to least significant. - $orderedUnits = @($script:UnitMap.Keys) + $orderedUnits = [System.Collections.ArrayList]::new() + foreach ($key in $script:UnitMap.Keys) { + $orderedUnits.Add($key) | Out-Null + } if ($Precision -eq 1) { # For precision=1, use the "fractional" approach. @@ -88,12 +105,12 @@ $chosenUnit = $unit; break } } - if (-not $chosenUnit) { $chosenUnit = 'Microseconds' } + if (-not $chosenUnit) { $chosenUnit = 'Nanoseconds' } } $fractionalValue = $originalTicks / $script:UnitMap[$chosenUnit].Ticks $roundedValue = [math]::Round($fractionalValue, 0, [System.MidpointRounding]::AwayFromZero) - $formatted = Format-UnitValue -value $roundedValue -unit $chosenUnit -FullNames:$FullNames + $formatted = Format-UnitValue -value $roundedValue -unit $chosenUnit -FullNames:$FullNames -UseSymbols:$UseSymbols if ($isNegative) { $formatted = "-$formatted" } return $formatted } else { @@ -122,7 +139,7 @@ $value = [math]::Floor($remainder / $unitTicks) } $remainder = $remainder - ($value * $unitTicks) - $resultSegments += Format-UnitValue -value $value -unit $unit -FullNames:$FullNames + $resultSegments += Format-UnitValue -value $value -unit $unit -FullNames:$FullNames -UseSymbols:$UseSymbols } $formatted = $resultSegments -join ' ' if ($isNegative) { $formatted = "-$formatted" } diff --git a/src/variables/private/UnitMap.ps1 b/src/variables/private/UnitMap.ps1 index 1104bb0..2f39106 100644 --- a/src/variables/private/UnitMap.ps1 +++ b/src/variables/private/UnitMap.ps1 @@ -7,73 +7,92 @@ $script:UnitMap = [ordered]@{ 'Millennia' = @{ Singular = 'millennium' Plural = 'millennia' - Abbreviation = 'mil' + Abbreviation = 'mill' + Symbol = 'kyr' Ticks = [System.TimeSpan]::TicksPerDay * $script:AverageDaysInYear * 1000 } 'Centuries' = @{ Singular = 'century' Plural = 'centuries' Abbreviation = 'cent' + Symbol = 'c' Ticks = [System.TimeSpan]::TicksPerDay * $script:AverageDaysInYear * 100 } 'Decades' = @{ Singular = 'decade' Plural = 'decades' Abbreviation = 'dec' + Symbol = 'dec' Ticks = [System.TimeSpan]::TicksPerDay * $script:AverageDaysInYear * 10 } 'Years' = @{ Singular = 'year' Plural = 'years' - Abbreviation = 'y' + Abbreviation = 'yr' + Symbol = 'y' Ticks = [System.TimeSpan]::TicksPerDay * $script:AverageDaysInYear } 'Months' = @{ Singular = 'month' Plural = 'months' - Abbreviation = 'mo' + Abbreviation = 'mon' + Symbol = 'mo' Ticks = [System.TimeSpan]::TicksPerDay * $script:AverageDaysInMonth } 'Weeks' = @{ Singular = 'week' Plural = 'weeks' - Abbreviation = 'w' + Abbreviation = 'wk' + Symbol = 'wk' Ticks = [System.TimeSpan]::TicksPerDay * $script:DaysInWeek } 'Days' = @{ Singular = 'day' Plural = 'days' - Abbreviation = 'd' + Abbreviation = 'day' + Symbol = 'd' Ticks = [System.TimeSpan]::TicksPerDay } 'Hours' = @{ Singular = 'hour' Plural = 'hours' - Abbreviation = 'h' + Abbreviation = 'hr' + Symbol = 'h' Ticks = [System.TimeSpan]::TicksPerHour } 'Minutes' = @{ Singular = 'minute' Plural = 'minutes' Abbreviation = 'min' + Symbol = 'm' Ticks = [System.TimeSpan]::TicksPerMinute } 'Seconds' = @{ Singular = 'second' Plural = 'seconds' - Abbreviation = 's' + Abbreviation = 'sec' + Symbol = 's' Ticks = [System.TimeSpan]::TicksPerSecond } 'Milliseconds' = @{ Singular = 'millisecond' Plural = 'milliseconds' - Abbreviation = 'ms' + Abbreviation = 'msec' + Symbol = 'ms' Ticks = [System.TimeSpan]::TicksPerMillisecond } 'Microseconds' = @{ Singular = 'microsecond' Plural = 'microseconds' - Abbreviation = "$([char]0x00B5)s" + Abbreviation = 'usec' + Symbol = "$([char]0x00B5)s" Ticks = 10 } + 'Nanoseconds' = @{ + Singular = 'nanosecond' + Plural = 'nanoseconds' + Abbreviation = 'nsec' + Symbol = 'ns' + Ticks = 1 + } } diff --git a/tests/PSModuleTest.Tests.ps1 b/tests/PSModuleTest.Tests.ps1 index 0b49355..4d22a02 100644 --- a/tests/PSModuleTest.Tests.ps1 +++ b/tests/PSModuleTest.Tests.ps1 @@ -1,9 +1,15 @@ -Describe 'TimeSpan' { +BeforeAll { + . $PSScriptRoot/../src/variables/private/UnitMap.ps1 + . $PSScriptRoot/../src/functions/private/Format-UnitValue.ps1 + . $PSScriptRoot/../src/functions/public/Format-TimeSpan.ps1 +} + +Describe 'TimeSpan' { Describe 'Format-TimeSpan' { Context 'Format-TimeSpan - Basic Usage' { - It 'Format-TimeSpan - Formats 90 minutes as 2h' { + It 'Format-TimeSpan - Formats 90 minutes as 2hr' { $result = New-TimeSpan -Minutes 90 | Format-TimeSpan - $result | Should -Be '2h' + $result | Should -Be '2hr' } } @@ -31,7 +37,7 @@ Context 'Format-TimeSpan - BaseUnit' { It 'Format-TimeSpan - Forces formatting in seconds' { $result = [TimeSpan]::FromMinutes(2) | Format-TimeSpan -BaseUnit 'Seconds' - $result | Should -Be '120s' + $result | Should -Be '120sec' } # Test micro seconds It 'Format-TimeSpan - Forces formatting in microseconds' { @@ -41,8 +47,50 @@ # Test microsecond abbreviation It 'Format-TimeSpan - Forces formatting in microseconds with abbreviation' { $result = [TimeSpan]::FromMilliseconds(1) | Format-TimeSpan -BaseUnit 'Microseconds' + $result | Should -Be '1000usec' + } + } + + Context 'Format-TimeSpan - UseSymbols' { + It 'Format-TimeSpan - Formats 90 minutes using symbols' { + $result = New-TimeSpan -Minutes 90 | Format-TimeSpan -UseSymbols + $result | Should -Be '2h' + } + + It 'Format-TimeSpan - Formats 5 minutes using symbols' { + $result = New-TimeSpan -Minutes 5 | Format-TimeSpan -UseSymbols + $result | Should -Be '5m' + } + + It 'Format-TimeSpan - Formats seconds using symbols' { + $result = [TimeSpan]::FromMinutes(2) | Format-TimeSpan -BaseUnit 'Seconds' -UseSymbols + $result | Should -Be '120s' + } + + It 'Format-TimeSpan - Formats microseconds using symbols' { + $result = [TimeSpan]::FromMilliseconds(1) | Format-TimeSpan -BaseUnit 'Microseconds' -UseSymbols $result | Should -Be '1000µs' } + + It 'Format-TimeSpan - Formats negative 5 minutes using symbols' { + $result = New-TimeSpan -Minutes -5 | Format-TimeSpan -UseSymbols + $result | Should -Be '-5m' + } + + It 'Format-TimeSpan - Formats days using symbols' { + $result = New-TimeSpan -Days 3 | Format-TimeSpan -UseSymbols + $result | Should -Be '3d' + } + + It 'Format-TimeSpan - Formats weeks using symbols' { + $result = New-TimeSpan -Days 14 | Format-TimeSpan -UseSymbols + $result | Should -Be '2wk' + } + + It 'Format-TimeSpan - Formats milliseconds using symbols' { + $result = [TimeSpan]::FromMilliseconds(500) | Format-TimeSpan -UseSymbols + $result | Should -Be '500ms' + } } } } From 3e12c78710e1861811f2a9f2f8ccc66fd76e1e9a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 11 Jul 2025 11:39:15 +0000 Subject: [PATCH 03/12] Replace UseSymbols/FullNames with Format parameter defaulting to Symbol Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com> --- src/functions/private/Format-UnitValue.ps1 | 69 +++++++++++++--------- src/functions/public/Format-TimeSpan.ps1 | 37 ++++++------ tests/PSModuleTest.Tests.ps1 | 51 ++++++++++------ 3 files changed, 91 insertions(+), 66 deletions(-) diff --git a/src/functions/private/Format-UnitValue.ps1 b/src/functions/private/Format-UnitValue.ps1 index c46e620..0876170 100644 --- a/src/functions/private/Format-UnitValue.ps1 +++ b/src/functions/private/Format-UnitValue.ps1 @@ -5,32 +5,30 @@ .DESCRIPTION This function takes an integer value and a unit and returns a formatted string. - If the -FullNames switch is specified, the function uses the singular or plural full unit name. - If the -UseSymbols switch is specified, the function uses symbols instead of abbreviations. - Otherwise, it returns the value with the unit abbreviation. + The format can be specified as Symbol, Abbreviation, FullName, or FullNameWithAlternative. .EXAMPLE - Format-UnitValue -Value 5 -Unit 'Hours' + Format-UnitValue -Value 5 -Unit 'Hours' -Format Symbol Output: ```powershell - 5hr + 5h ``` - Returns the formatted value with its abbreviation. + Returns the formatted value with its symbol. .EXAMPLE - Format-UnitValue -Value 5 -Unit 'Hours' -UseSymbols + Format-UnitValue -Value 5 -Unit 'Hours' -Format Abbreviation Output: ```powershell - 5h + 5hr ``` - Returns the formatted value with its symbol. + Returns the formatted value with its abbreviation. .EXAMPLE - Format-UnitValue -Value 1 -Unit 'Hours' -FullNames + Format-UnitValue -Value 1 -Unit 'Hours' -Format FullName Output: ```powershell @@ -40,7 +38,7 @@ Returns the formatted value with the full singular unit name. .EXAMPLE - Format-UnitValue -Value 2 -Unit 'Hours' -FullNames + Format-UnitValue -Value 2 -Unit 'Hours' -Format FullName Output: ```powershell @@ -49,8 +47,18 @@ Returns the formatted value with the full plural unit name. + .EXAMPLE + Format-UnitValue -Value 5 -Unit 'Hours' -Format FullNameWithAlternative + + Output: + ```powershell + 5 hours/hour + ``` + + Returns the formatted value with both plural and singular forms. + .OUTPUTS - string. A formatted string combining the value and its corresponding unit abbreviation, symbol, or full name. + string. A formatted string combining the value and its corresponding unit in the specified format. .LINK https://psmodule.io/Format/Functions/Format-UnitValue/ @@ -66,24 +74,29 @@ [Parameter(Mandatory)] [string] $Unit, - # Switch to use full unit names instead of abbreviations or symbols. + # The format for displaying the unit. [Parameter()] - [switch] $FullNames, - - # Switch to use symbols instead of abbreviations. - [Parameter()] - [switch] $UseSymbols + [ValidateSet('Symbol', 'Abbreviation', 'FullName', 'FullNameWithAlternative')] + [string] $Format = 'Symbol' ) - if ($FullNames) { - # Choose singular or plural form based on the value. - $unitName = if ($Value -eq 1) { $script:UnitMap[$Unit].Singular } else { $script:UnitMap[$Unit].Plural } - return "$Value $unitName" - } - - if ($UseSymbols) { - "$Value$($script:UnitMap[$Unit].Symbol)" - } else { - "$Value$($script:UnitMap[$Unit].Abbreviation)" + switch ($Format) { + 'FullName' { + # Choose singular or plural form based on the value. + $unitName = if ($Value -eq 1) { $script:UnitMap[$Unit].Singular } else { $script:UnitMap[$Unit].Plural } + return "$Value $unitName" + } + 'FullNameWithAlternative' { + # Show both plural and singular forms. + $plural = $script:UnitMap[$Unit].Plural + $singular = $script:UnitMap[$Unit].Singular + return "$Value $plural/$singular" + } + 'Abbreviation' { + return "$Value$($script:UnitMap[$Unit].Abbreviation)" + } + 'Symbol' { + return "$Value$($script:UnitMap[$Unit].Symbol)" + } } } diff --git a/src/functions/public/Format-TimeSpan.ps1 b/src/functions/public/Format-TimeSpan.ps1 index 72f2ed9..6d9f0fa 100644 --- a/src/functions/public/Format-TimeSpan.ps1 +++ b/src/functions/public/Format-TimeSpan.ps1 @@ -5,48 +5,48 @@ .DESCRIPTION This function converts a TimeSpan object into a formatted string based on a chosen unit or precision. - It allows specifying a base unit, the number of precision levels, and whether to use full unit names, - symbols, or abbreviations. If the TimeSpan is negative, it is prefixed with a minus sign. + It allows specifying a base unit, the number of precision levels, and the format for displaying units. + If the TimeSpan is negative, it is prefixed with a minus sign. .EXAMPLE New-TimeSpan -Minutes 90 | Format-TimeSpan Output: ```powershell - 2hr + 2h ``` - Formats the given TimeSpan into a human-readable format using the most significant unit. + Formats the given TimeSpan into a human-readable format using the most significant unit with symbols (default). .EXAMPLE - New-TimeSpan -Minutes 90 | Format-TimeSpan -UseSymbols + New-TimeSpan -Minutes 90 | Format-TimeSpan -Format Abbreviation Output: ```powershell - 2h + 2hr ``` - Formats the given TimeSpan using symbols instead of abbreviations. + Formats the given TimeSpan using abbreviations instead of symbols. .EXAMPLE - [TimeSpan]::FromSeconds(3661) | Format-TimeSpan -Precision 2 -FullNames + [TimeSpan]::FromSeconds(3661) | Format-TimeSpan -Precision 2 -Format FullName Output: ```powershell 1 hour 1 minute ``` - Returns the TimeSpan formatted into multiple components based on the specified precision. + Returns the TimeSpan formatted into multiple components using full unit names. .EXAMPLE - [TimeSpan]::FromMilliseconds(500) | Format-TimeSpan -Precision 2 -FullNames + [TimeSpan]::FromMilliseconds(500) | Format-TimeSpan -Precision 2 -Format FullNameWithAlternative Output: ```powershell - 500 milliseconds 0 microseconds + 500 milliseconds/millisecond 0 microseconds/microsecond ``` - Forces the output to be formatted in milliseconds, regardless of precision. + Returns the TimeSpan formatted using the full name with alternative format. .OUTPUTS System.String @@ -71,13 +71,10 @@ [Parameter()] [string] $BaseUnit, - # If specified, outputs full unit names instead of abbreviations or symbols. - [Parameter()] - [switch] $FullNames, - - # If specified, uses symbols instead of abbreviations for unit formatting. + # Specifies the format for displaying time units. [Parameter()] - [switch] $UseSymbols + [ValidateSet('Symbol', 'Abbreviation', 'FullName', 'FullNameWithAlternative')] + [string] $Format = 'Symbol' ) process { @@ -110,7 +107,7 @@ $fractionalValue = $originalTicks / $script:UnitMap[$chosenUnit].Ticks $roundedValue = [math]::Round($fractionalValue, 0, [System.MidpointRounding]::AwayFromZero) - $formatted = Format-UnitValue -value $roundedValue -unit $chosenUnit -FullNames:$FullNames -UseSymbols:$UseSymbols + $formatted = Format-UnitValue -value $roundedValue -unit $chosenUnit -Format $Format if ($isNegative) { $formatted = "-$formatted" } return $formatted } else { @@ -139,7 +136,7 @@ $value = [math]::Floor($remainder / $unitTicks) } $remainder = $remainder - ($value * $unitTicks) - $resultSegments += Format-UnitValue -value $value -unit $unit -FullNames:$FullNames -UseSymbols:$UseSymbols + $resultSegments += Format-UnitValue -value $value -unit $unit -Format $Format } $formatted = $resultSegments -join ' ' if ($isNegative) { $formatted = "-$formatted" } diff --git a/tests/PSModuleTest.Tests.ps1 b/tests/PSModuleTest.Tests.ps1 index 4d22a02..ec0c988 100644 --- a/tests/PSModuleTest.Tests.ps1 +++ b/tests/PSModuleTest.Tests.ps1 @@ -7,22 +7,22 @@ Describe 'TimeSpan' { Describe 'Format-TimeSpan' { Context 'Format-TimeSpan - Basic Usage' { - It 'Format-TimeSpan - Formats 90 minutes as 2hr' { + It 'Format-TimeSpan - Formats 90 minutes as 2h (default symbols)' { $result = New-TimeSpan -Minutes 90 | Format-TimeSpan - $result | Should -Be '2hr' + $result | Should -Be '2h' } } Context 'Format-TimeSpan - Precision and FullNames' { It 'Format-TimeSpan - Formats 3661 seconds with precision 2 and full names' { - $result = [TimeSpan]::FromSeconds(3661) | Format-TimeSpan -Precision 2 -FullNames + $result = [TimeSpan]::FromSeconds(3661) | Format-TimeSpan -Precision 2 -Format FullName $result | Should -Be '1 hour 1 minute' } } Context 'Format-TimeSpan - Smallest Unit Handling' { It 'Format-TimeSpan - Formats 500 milliseconds with precision 2 and full names' { - $result = [TimeSpan]::FromMilliseconds(500) | Format-TimeSpan -Precision 2 -FullNames + $result = [TimeSpan]::FromMilliseconds(500) | Format-TimeSpan -Precision 2 -Format FullName $result | Should -Be '500 milliseconds 0 microseconds' } } @@ -30,67 +30,82 @@ Describe 'TimeSpan' { Context 'Format-TimeSpan - Negative TimeSpan' { It 'Format-TimeSpan - Formats negative 5 minutes correctly' { $result = New-TimeSpan -Minutes -5 | Format-TimeSpan - $result | Should -Be '-5min' + $result | Should -Be '-5m' } } Context 'Format-TimeSpan - BaseUnit' { It 'Format-TimeSpan - Forces formatting in seconds' { $result = [TimeSpan]::FromMinutes(2) | Format-TimeSpan -BaseUnit 'Seconds' - $result | Should -Be '120sec' + $result | Should -Be '120s' } # Test micro seconds It 'Format-TimeSpan - Forces formatting in microseconds' { - $result = [TimeSpan]::FromMilliseconds(1) | Format-TimeSpan -BaseUnit 'Microseconds' -FullNames + $result = [TimeSpan]::FromMilliseconds(1) | Format-TimeSpan -BaseUnit 'Microseconds' -Format FullName $result | Should -Be '1000 microseconds' } # Test microsecond abbreviation It 'Format-TimeSpan - Forces formatting in microseconds with abbreviation' { - $result = [TimeSpan]::FromMilliseconds(1) | Format-TimeSpan -BaseUnit 'Microseconds' + $result = [TimeSpan]::FromMilliseconds(1) | Format-TimeSpan -BaseUnit 'Microseconds' -Format Abbreviation $result | Should -Be '1000usec' } } - Context 'Format-TimeSpan - UseSymbols' { - It 'Format-TimeSpan - Formats 90 minutes using symbols' { - $result = New-TimeSpan -Minutes 90 | Format-TimeSpan -UseSymbols + Context 'Format-TimeSpan - Format Parameter' { + It 'Format-TimeSpan - Formats 90 minutes using symbols (default)' { + $result = New-TimeSpan -Minutes 90 | Format-TimeSpan -Format Symbol $result | Should -Be '2h' } + It 'Format-TimeSpan - Formats 90 minutes using abbreviations' { + $result = New-TimeSpan -Minutes 90 | Format-TimeSpan -Format Abbreviation + $result | Should -Be '2hr' + } + It 'Format-TimeSpan - Formats 5 minutes using symbols' { - $result = New-TimeSpan -Minutes 5 | Format-TimeSpan -UseSymbols + $result = New-TimeSpan -Minutes 5 | Format-TimeSpan -Format Symbol $result | Should -Be '5m' } It 'Format-TimeSpan - Formats seconds using symbols' { - $result = [TimeSpan]::FromMinutes(2) | Format-TimeSpan -BaseUnit 'Seconds' -UseSymbols + $result = [TimeSpan]::FromMinutes(2) | Format-TimeSpan -BaseUnit 'Seconds' -Format Symbol $result | Should -Be '120s' } It 'Format-TimeSpan - Formats microseconds using symbols' { - $result = [TimeSpan]::FromMilliseconds(1) | Format-TimeSpan -BaseUnit 'Microseconds' -UseSymbols + $result = [TimeSpan]::FromMilliseconds(1) | Format-TimeSpan -BaseUnit 'Microseconds' -Format Symbol $result | Should -Be '1000µs' } It 'Format-TimeSpan - Formats negative 5 minutes using symbols' { - $result = New-TimeSpan -Minutes -5 | Format-TimeSpan -UseSymbols + $result = New-TimeSpan -Minutes -5 | Format-TimeSpan -Format Symbol $result | Should -Be '-5m' } It 'Format-TimeSpan - Formats days using symbols' { - $result = New-TimeSpan -Days 3 | Format-TimeSpan -UseSymbols + $result = New-TimeSpan -Days 3 | Format-TimeSpan -Format Symbol $result | Should -Be '3d' } It 'Format-TimeSpan - Formats weeks using symbols' { - $result = New-TimeSpan -Days 14 | Format-TimeSpan -UseSymbols + $result = New-TimeSpan -Days 14 | Format-TimeSpan -Format Symbol $result | Should -Be '2wk' } It 'Format-TimeSpan - Formats milliseconds using symbols' { - $result = [TimeSpan]::FromMilliseconds(500) | Format-TimeSpan -UseSymbols + $result = [TimeSpan]::FromMilliseconds(500) | Format-TimeSpan -Format Symbol $result | Should -Be '500ms' } + + It 'Format-TimeSpan - Formats using FullNameWithAlternative format' { + $result = New-TimeSpan -Minutes 5 | Format-TimeSpan -Format FullNameWithAlternative + $result | Should -Be '5 minutes/minute' + } + + It 'Format-TimeSpan - Formats using FullNameWithAlternative format with precision' { + $result = [TimeSpan]::FromSeconds(3661) | Format-TimeSpan -Precision 2 -Format FullNameWithAlternative + $result | Should -Be '1 hours/hour 1 minutes/minute' + } } } } From 194be116184056b7b652d2162400be85c4475a8e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 11 Jul 2025 12:20:15 +0000 Subject: [PATCH 04/12] Fix nanoseconds ticks and FullNameWithAlternative format based on feedback Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com> --- src/functions/private/Format-UnitValue.ps1 | 11 +++++------ src/functions/public/Format-TimeSpan.ps1 | 4 ++-- src/variables/private/UnitMap.ps1 | 7 ------- tests/PSModuleTest.Tests.ps1 | 4 ++-- 4 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/functions/private/Format-UnitValue.ps1 b/src/functions/private/Format-UnitValue.ps1 index 0876170..a79f6fe 100644 --- a/src/functions/private/Format-UnitValue.ps1 +++ b/src/functions/private/Format-UnitValue.ps1 @@ -52,10 +52,10 @@ Output: ```powershell - 5 hours/hour + 5 hours ``` - Returns the formatted value with both plural and singular forms. + Returns the formatted value choosing the appropriate singular or plural form. .OUTPUTS string. A formatted string combining the value and its corresponding unit in the specified format. @@ -87,10 +87,9 @@ return "$Value $unitName" } 'FullNameWithAlternative' { - # Show both plural and singular forms. - $plural = $script:UnitMap[$Unit].Plural - $singular = $script:UnitMap[$Unit].Singular - return "$Value $plural/$singular" + # Choose singular or plural form based on the value. + $unitName = if ($Value -eq 1) { $script:UnitMap[$Unit].Singular } else { $script:UnitMap[$Unit].Plural } + return "$Value $unitName" } 'Abbreviation' { return "$Value$($script:UnitMap[$Unit].Abbreviation)" diff --git a/src/functions/public/Format-TimeSpan.ps1 b/src/functions/public/Format-TimeSpan.ps1 index 6d9f0fa..895f1ce 100644 --- a/src/functions/public/Format-TimeSpan.ps1 +++ b/src/functions/public/Format-TimeSpan.ps1 @@ -43,7 +43,7 @@ Output: ```powershell - 500 milliseconds/millisecond 0 microseconds/microsecond + 500 milliseconds 0 microseconds ``` Returns the TimeSpan formatted using the full name with alternative format. @@ -102,7 +102,7 @@ $chosenUnit = $unit; break } } - if (-not $chosenUnit) { $chosenUnit = 'Nanoseconds' } + if (-not $chosenUnit) { $chosenUnit = 'Microseconds' } } $fractionalValue = $originalTicks / $script:UnitMap[$chosenUnit].Ticks diff --git a/src/variables/private/UnitMap.ps1 b/src/variables/private/UnitMap.ps1 index 2f39106..ad72b17 100644 --- a/src/variables/private/UnitMap.ps1 +++ b/src/variables/private/UnitMap.ps1 @@ -88,11 +88,4 @@ $script:UnitMap = [ordered]@{ Symbol = "$([char]0x00B5)s" Ticks = 10 } - 'Nanoseconds' = @{ - Singular = 'nanosecond' - Plural = 'nanoseconds' - Abbreviation = 'nsec' - Symbol = 'ns' - Ticks = 1 - } } diff --git a/tests/PSModuleTest.Tests.ps1 b/tests/PSModuleTest.Tests.ps1 index ec0c988..dd27a63 100644 --- a/tests/PSModuleTest.Tests.ps1 +++ b/tests/PSModuleTest.Tests.ps1 @@ -99,12 +99,12 @@ Describe 'TimeSpan' { It 'Format-TimeSpan - Formats using FullNameWithAlternative format' { $result = New-TimeSpan -Minutes 5 | Format-TimeSpan -Format FullNameWithAlternative - $result | Should -Be '5 minutes/minute' + $result | Should -Be '5 minutes' } It 'Format-TimeSpan - Formats using FullNameWithAlternative format with precision' { $result = [TimeSpan]::FromSeconds(3661) | Format-TimeSpan -Precision 2 -Format FullNameWithAlternative - $result | Should -Be '1 hours/hour 1 minutes/minute' + $result | Should -Be '1 hour 1 minute' } } } From 3ffcf95f683f63a04f2a2225af4c48c8d63ac5b0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 11 Jul 2025 12:40:34 +0000 Subject: [PATCH 05/12] Remove FullNameWithAlternative format option as requested Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com> --- src/functions/private/Format-UnitValue.ps1 | 15 +-------------- src/functions/public/Format-TimeSpan.ps1 | 10 +--------- tests/PSModuleTest.Tests.ps1 | 8 -------- 3 files changed, 2 insertions(+), 31 deletions(-) diff --git a/src/functions/private/Format-UnitValue.ps1 b/src/functions/private/Format-UnitValue.ps1 index a79f6fe..4460a21 100644 --- a/src/functions/private/Format-UnitValue.ps1 +++ b/src/functions/private/Format-UnitValue.ps1 @@ -47,15 +47,7 @@ Returns the formatted value with the full plural unit name. - .EXAMPLE - Format-UnitValue -Value 5 -Unit 'Hours' -Format FullNameWithAlternative - - Output: - ```powershell - 5 hours - ``` - Returns the formatted value choosing the appropriate singular or plural form. .OUTPUTS string. A formatted string combining the value and its corresponding unit in the specified format. @@ -76,7 +68,7 @@ # The format for displaying the unit. [Parameter()] - [ValidateSet('Symbol', 'Abbreviation', 'FullName', 'FullNameWithAlternative')] + [ValidateSet('Symbol', 'Abbreviation', 'FullName')] [string] $Format = 'Symbol' ) @@ -86,11 +78,6 @@ $unitName = if ($Value -eq 1) { $script:UnitMap[$Unit].Singular } else { $script:UnitMap[$Unit].Plural } return "$Value $unitName" } - 'FullNameWithAlternative' { - # Choose singular or plural form based on the value. - $unitName = if ($Value -eq 1) { $script:UnitMap[$Unit].Singular } else { $script:UnitMap[$Unit].Plural } - return "$Value $unitName" - } 'Abbreviation' { return "$Value$($script:UnitMap[$Unit].Abbreviation)" } diff --git a/src/functions/public/Format-TimeSpan.ps1 b/src/functions/public/Format-TimeSpan.ps1 index 895f1ce..7c7d36e 100644 --- a/src/functions/public/Format-TimeSpan.ps1 +++ b/src/functions/public/Format-TimeSpan.ps1 @@ -38,15 +38,7 @@ Returns the TimeSpan formatted into multiple components using full unit names. - .EXAMPLE - [TimeSpan]::FromMilliseconds(500) | Format-TimeSpan -Precision 2 -Format FullNameWithAlternative - - Output: - ```powershell - 500 milliseconds 0 microseconds - ``` - Returns the TimeSpan formatted using the full name with alternative format. .OUTPUTS System.String @@ -73,7 +65,7 @@ # Specifies the format for displaying time units. [Parameter()] - [ValidateSet('Symbol', 'Abbreviation', 'FullName', 'FullNameWithAlternative')] + [ValidateSet('Symbol', 'Abbreviation', 'FullName')] [string] $Format = 'Symbol' ) diff --git a/tests/PSModuleTest.Tests.ps1 b/tests/PSModuleTest.Tests.ps1 index dd27a63..b6d9a96 100644 --- a/tests/PSModuleTest.Tests.ps1 +++ b/tests/PSModuleTest.Tests.ps1 @@ -97,15 +97,7 @@ Describe 'TimeSpan' { $result | Should -Be '500ms' } - It 'Format-TimeSpan - Formats using FullNameWithAlternative format' { - $result = New-TimeSpan -Minutes 5 | Format-TimeSpan -Format FullNameWithAlternative - $result | Should -Be '5 minutes' - } - It 'Format-TimeSpan - Formats using FullNameWithAlternative format with precision' { - $result = [TimeSpan]::FromSeconds(3661) | Format-TimeSpan -Precision 2 -Format FullNameWithAlternative - $result | Should -Be '1 hour 1 minute' - } } } } From 46415025e6b80047d061d887fce85b51fb24149a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 13 Jul 2025 20:59:57 +0000 Subject: [PATCH 06/12] Merge Format-UnitValue into Format-TimeSpan and follow OTBS bracing style Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com> --- src/functions/private/Format-UnitValue.ps1 | 88 ---------------------- src/functions/public/Format-TimeSpan.ps1 | 27 ++++++- tests/PSModuleTest.Tests.ps1 | 1 - 3 files changed, 25 insertions(+), 91 deletions(-) delete mode 100644 src/functions/private/Format-UnitValue.ps1 diff --git a/src/functions/private/Format-UnitValue.ps1 b/src/functions/private/Format-UnitValue.ps1 deleted file mode 100644 index 4460a21..0000000 --- a/src/functions/private/Format-UnitValue.ps1 +++ /dev/null @@ -1,88 +0,0 @@ -function Format-UnitValue { - <# - .SYNOPSIS - Formats a numerical value with its corresponding unit. - - .DESCRIPTION - This function takes an integer value and a unit and returns a formatted string. - The format can be specified as Symbol, Abbreviation, FullName, or FullNameWithAlternative. - - .EXAMPLE - Format-UnitValue -Value 5 -Unit 'Hours' -Format Symbol - - Output: - ```powershell - 5h - ``` - - Returns the formatted value with its symbol. - - .EXAMPLE - Format-UnitValue -Value 5 -Unit 'Hours' -Format Abbreviation - - Output: - ```powershell - 5hr - ``` - - Returns the formatted value with its abbreviation. - - .EXAMPLE - Format-UnitValue -Value 1 -Unit 'Hours' -Format FullName - - Output: - ```powershell - 1 hour - ``` - - Returns the formatted value with the full singular unit name. - - .EXAMPLE - Format-UnitValue -Value 2 -Unit 'Hours' -Format FullName - - Output: - ```powershell - 2 hours - ``` - - Returns the formatted value with the full plural unit name. - - - - .OUTPUTS - string. A formatted string combining the value and its corresponding unit in the specified format. - - .LINK - https://psmodule.io/Format/Functions/Format-UnitValue/ - #> - [OutputType([string])] - [CmdletBinding()] - param( - # The numerical value to be formatted with a unit. - [Parameter(Mandatory)] - [System.Int128] $Value, - - # The unit type to append to the value. - [Parameter(Mandatory)] - [string] $Unit, - - # The format for displaying the unit. - [Parameter()] - [ValidateSet('Symbol', 'Abbreviation', 'FullName')] - [string] $Format = 'Symbol' - ) - - switch ($Format) { - 'FullName' { - # Choose singular or plural form based on the value. - $unitName = if ($Value -eq 1) { $script:UnitMap[$Unit].Singular } else { $script:UnitMap[$Unit].Plural } - return "$Value $unitName" - } - 'Abbreviation' { - return "$Value$($script:UnitMap[$Unit].Abbreviation)" - } - 'Symbol' { - return "$Value$($script:UnitMap[$Unit].Symbol)" - } - } -} diff --git a/src/functions/public/Format-TimeSpan.ps1 b/src/functions/public/Format-TimeSpan.ps1 index 7c7d36e..c95fca6 100644 --- a/src/functions/public/Format-TimeSpan.ps1 +++ b/src/functions/public/Format-TimeSpan.ps1 @@ -70,6 +70,29 @@ ) process { + # Helper function to format a value with its unit + function Format-UnitValue { + param( + [System.Int128] $Value, + [string] $Unit, + [string] $Format + ) + + switch ($Format) { + 'FullName' { + # Choose singular or plural form based on the value. + $unitName = if ($Value -eq 1) { $script:UnitMap[$Unit].Singular } else { $script:UnitMap[$Unit].Plural } + return "$Value $unitName" + } + 'Abbreviation' { + return "$Value$($script:UnitMap[$Unit].Abbreviation)" + } + 'Symbol' { + return "$Value$($script:UnitMap[$Unit].Symbol)" + } + } + } + $isNegative = $TimeSpan.Ticks -lt 0 if ($isNegative) { $TimeSpan = [System.TimeSpan]::FromTicks(-1 * $TimeSpan.Ticks) @@ -99,7 +122,7 @@ $fractionalValue = $originalTicks / $script:UnitMap[$chosenUnit].Ticks $roundedValue = [math]::Round($fractionalValue, 0, [System.MidpointRounding]::AwayFromZero) - $formatted = Format-UnitValue -value $roundedValue -unit $chosenUnit -Format $Format + $formatted = Format-UnitValue -Value $roundedValue -Unit $chosenUnit -Format $Format if ($isNegative) { $formatted = "-$formatted" } return $formatted } else { @@ -128,7 +151,7 @@ $value = [math]::Floor($remainder / $unitTicks) } $remainder = $remainder - ($value * $unitTicks) - $resultSegments += Format-UnitValue -value $value -unit $unit -Format $Format + $resultSegments += Format-UnitValue -Value $value -Unit $unit -Format $Format } $formatted = $resultSegments -join ' ' if ($isNegative) { $formatted = "-$formatted" } diff --git a/tests/PSModuleTest.Tests.ps1 b/tests/PSModuleTest.Tests.ps1 index b6d9a96..fa9ac60 100644 --- a/tests/PSModuleTest.Tests.ps1 +++ b/tests/PSModuleTest.Tests.ps1 @@ -1,6 +1,5 @@ BeforeAll { . $PSScriptRoot/../src/variables/private/UnitMap.ps1 - . $PSScriptRoot/../src/functions/private/Format-UnitValue.ps1 . $PSScriptRoot/../src/functions/public/Format-TimeSpan.ps1 } From f53056e2a44e4054c94b1f08472aa434dc6737ca Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 13 Jul 2025 21:12:06 +0000 Subject: [PATCH 07/12] Move Format-UnitValue to private function with OTBS style Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com> --- src/functions/private/Format-UnitValue.ps1 | 86 ++++++++++++++++++++++ src/functions/public/Format-TimeSpan.ps1 | 22 ------ tests/PSModuleTest.Tests.ps1 | 1 + 3 files changed, 87 insertions(+), 22 deletions(-) create mode 100644 src/functions/private/Format-UnitValue.ps1 diff --git a/src/functions/private/Format-UnitValue.ps1 b/src/functions/private/Format-UnitValue.ps1 new file mode 100644 index 0000000..da345d7 --- /dev/null +++ b/src/functions/private/Format-UnitValue.ps1 @@ -0,0 +1,86 @@ +function Format-UnitValue { + <# + .SYNOPSIS + Formats a numerical value with its corresponding unit. + + .DESCRIPTION + This function takes an integer value and a unit and returns a formatted string. + The format can be specified as Symbol, Abbreviation, or FullName. + + .EXAMPLE + Format-UnitValue -Value 5 -Unit 'Hours' -Format Symbol + + Output: + ```powershell + 5h + ``` + + Returns the formatted value with its symbol. + + .EXAMPLE + Format-UnitValue -Value 5 -Unit 'Hours' -Format Abbreviation + + Output: + ```powershell + 5hr + ``` + + Returns the formatted value with its abbreviation. + + .EXAMPLE + Format-UnitValue -Value 1 -Unit 'Hours' -Format FullName + + Output: + ```powershell + 1 hour + ``` + + Returns the formatted value with the full singular unit name. + + .EXAMPLE + Format-UnitValue -Value 2 -Unit 'Hours' -Format FullName + + Output: + ```powershell + 2 hours + ``` + + Returns the formatted value with the full plural unit name. + + .OUTPUTS + string. A formatted string combining the value and its corresponding unit in the specified format. + + .LINK + https://psmodule.io/TimeSpan/Functions/Format-UnitValue/ + #> + [OutputType([string])] + [CmdletBinding()] + param( + # The numerical value to be formatted with a unit. + [Parameter(Mandatory)] + [System.Int128] $Value, + + # The unit type to append to the value. + [Parameter(Mandatory)] + [string] $Unit, + + # The format for displaying the unit. + [Parameter()] + [ValidateSet('Symbol', 'Abbreviation', 'FullName')] + [string] $Format = 'Symbol' + ) + + switch ($Format) { + 'FullName' { + # Choose singular or plural form based on the value. + $unitName = if ($Value -eq 1) { $script:UnitMap[$Unit].Singular } else { $script:UnitMap[$Unit].Plural } + return "$Value $unitName" + } + 'Abbreviation' { + return "$Value$($script:UnitMap[$Unit].Abbreviation)" + } + 'Symbol' { + return "$Value$($script:UnitMap[$Unit].Symbol)" + } + } +} \ No newline at end of file diff --git a/src/functions/public/Format-TimeSpan.ps1 b/src/functions/public/Format-TimeSpan.ps1 index c95fca6..a191b8f 100644 --- a/src/functions/public/Format-TimeSpan.ps1 +++ b/src/functions/public/Format-TimeSpan.ps1 @@ -70,28 +70,6 @@ ) process { - # Helper function to format a value with its unit - function Format-UnitValue { - param( - [System.Int128] $Value, - [string] $Unit, - [string] $Format - ) - - switch ($Format) { - 'FullName' { - # Choose singular or plural form based on the value. - $unitName = if ($Value -eq 1) { $script:UnitMap[$Unit].Singular } else { $script:UnitMap[$Unit].Plural } - return "$Value $unitName" - } - 'Abbreviation' { - return "$Value$($script:UnitMap[$Unit].Abbreviation)" - } - 'Symbol' { - return "$Value$($script:UnitMap[$Unit].Symbol)" - } - } - } $isNegative = $TimeSpan.Ticks -lt 0 if ($isNegative) { diff --git a/tests/PSModuleTest.Tests.ps1 b/tests/PSModuleTest.Tests.ps1 index fa9ac60..b6d9a96 100644 --- a/tests/PSModuleTest.Tests.ps1 +++ b/tests/PSModuleTest.Tests.ps1 @@ -1,5 +1,6 @@ BeforeAll { . $PSScriptRoot/../src/variables/private/UnitMap.ps1 + . $PSScriptRoot/../src/functions/private/Format-UnitValue.ps1 . $PSScriptRoot/../src/functions/public/Format-TimeSpan.ps1 } From c5afa879b29238e0ba89f982b6d741611f8d217d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 13 Jul 2025 21:17:12 +0000 Subject: [PATCH 08/12] Replace | Out-Null with $null = assignment for better performance Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com> --- src/functions/public/Format-TimeSpan.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions/public/Format-TimeSpan.ps1 b/src/functions/public/Format-TimeSpan.ps1 index a191b8f..8651c47 100644 --- a/src/functions/public/Format-TimeSpan.ps1 +++ b/src/functions/public/Format-TimeSpan.ps1 @@ -80,7 +80,7 @@ # Ordered list of units from most to least significant. $orderedUnits = [System.Collections.ArrayList]::new() foreach ($key in $script:UnitMap.Keys) { - $orderedUnits.Add($key) | Out-Null + $null = $orderedUnits.Add($key) } if ($Precision -eq 1) { From 6146f45d89fba891692bca3786fd2115338deb23 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 13 Jul 2025 22:28:44 +0000 Subject: [PATCH 09/12] Fix trailing whitespace in test file Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com> --- tests/PSModuleTest.Tests.ps1 | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/PSModuleTest.Tests.ps1 b/tests/PSModuleTest.Tests.ps1 index b6d9a96..36ea36e 100644 --- a/tests/PSModuleTest.Tests.ps1 +++ b/tests/PSModuleTest.Tests.ps1 @@ -56,27 +56,22 @@ Describe 'TimeSpan' { $result = New-TimeSpan -Minutes 90 | Format-TimeSpan -Format Symbol $result | Should -Be '2h' } - It 'Format-TimeSpan - Formats 90 minutes using abbreviations' { $result = New-TimeSpan -Minutes 90 | Format-TimeSpan -Format Abbreviation $result | Should -Be '2hr' } - It 'Format-TimeSpan - Formats 5 minutes using symbols' { $result = New-TimeSpan -Minutes 5 | Format-TimeSpan -Format Symbol $result | Should -Be '5m' } - It 'Format-TimeSpan - Formats seconds using symbols' { $result = [TimeSpan]::FromMinutes(2) | Format-TimeSpan -BaseUnit 'Seconds' -Format Symbol $result | Should -Be '120s' } - It 'Format-TimeSpan - Formats microseconds using symbols' { $result = [TimeSpan]::FromMilliseconds(1) | Format-TimeSpan -BaseUnit 'Microseconds' -Format Symbol $result | Should -Be '1000µs' } - It 'Format-TimeSpan - Formats negative 5 minutes using symbols' { $result = New-TimeSpan -Minutes -5 | Format-TimeSpan -Format Symbol $result | Should -Be '-5m' From 444fb2195179cbcb79bddbdf73c35fcd97677032 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 13 Jul 2025 22:53:32 +0000 Subject: [PATCH 10/12] Address test file feedback: remove BeforeAll block, update micro symbol test, remove blank lines Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com> --- tests/PSModuleTest.Tests.ps1 | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/tests/PSModuleTest.Tests.ps1 b/tests/PSModuleTest.Tests.ps1 index 36ea36e..5ea51af 100644 --- a/tests/PSModuleTest.Tests.ps1 +++ b/tests/PSModuleTest.Tests.ps1 @@ -1,8 +1,4 @@ -BeforeAll { - . $PSScriptRoot/../src/variables/private/UnitMap.ps1 - . $PSScriptRoot/../src/functions/private/Format-UnitValue.ps1 - . $PSScriptRoot/../src/functions/public/Format-TimeSpan.ps1 -} + Describe 'TimeSpan' { Describe 'Format-TimeSpan' { @@ -44,10 +40,10 @@ Describe 'TimeSpan' { $result = [TimeSpan]::FromMilliseconds(1) | Format-TimeSpan -BaseUnit 'Microseconds' -Format FullName $result | Should -Be '1000 microseconds' } - # Test microsecond abbreviation - It 'Format-TimeSpan - Forces formatting in microseconds with abbreviation' { - $result = [TimeSpan]::FromMilliseconds(1) | Format-TimeSpan -BaseUnit 'Microseconds' -Format Abbreviation - $result | Should -Be '1000usec' + # Test microsecond symbol + It 'Format-TimeSpan - Forces formatting in microseconds with symbol' { + $result = [TimeSpan]::FromMilliseconds(1) | Format-TimeSpan -BaseUnit 'Microseconds' -Format Symbol + $result | Should -Be "1000$([char]0x00B5)s" } } @@ -91,8 +87,6 @@ Describe 'TimeSpan' { $result = [TimeSpan]::FromMilliseconds(500) | Format-TimeSpan -Format Symbol $result | Should -Be '500ms' } - - } } } From 0702dad9a5ca658d58fe50a372a08da4dbf16d1f Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Mon, 14 Jul 2025 09:32:16 +0200 Subject: [PATCH 11/12] Fix formatting issues in Format-UnitValue and Format-TimeSpan functions --- src/functions/private/Format-UnitValue.ps1 | 4 ++-- src/functions/public/Format-TimeSpan.ps1 | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/functions/private/Format-UnitValue.ps1 b/src/functions/private/Format-UnitValue.ps1 index da345d7..eebdb48 100644 --- a/src/functions/private/Format-UnitValue.ps1 +++ b/src/functions/private/Format-UnitValue.ps1 @@ -1,4 +1,4 @@ -function Format-UnitValue { +function Format-UnitValue { <# .SYNOPSIS Formats a numerical value with its corresponding unit. @@ -83,4 +83,4 @@ function Format-UnitValue { return "$Value$($script:UnitMap[$Unit].Symbol)" } } -} \ No newline at end of file +} diff --git a/src/functions/public/Format-TimeSpan.ps1 b/src/functions/public/Format-TimeSpan.ps1 index 8651c47..da88b47 100644 --- a/src/functions/public/Format-TimeSpan.ps1 +++ b/src/functions/public/Format-TimeSpan.ps1 @@ -70,7 +70,6 @@ ) process { - $isNegative = $TimeSpan.Ticks -lt 0 if ($isNegative) { $TimeSpan = [System.TimeSpan]::FromTicks(-1 * $TimeSpan.Ticks) From 39fc822376b949627ac73ceafbc8e10f3d04279f Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Mon, 14 Jul 2025 09:37:56 +0200 Subject: [PATCH 12/12] Update Microseconds unit abbreviation and symbol for consistency --- src/variables/private/UnitMap.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/variables/private/UnitMap.ps1 b/src/variables/private/UnitMap.ps1 index ad72b17..34a4e05 100644 --- a/src/variables/private/UnitMap.ps1 +++ b/src/variables/private/UnitMap.ps1 @@ -84,8 +84,8 @@ $script:UnitMap = [ordered]@{ 'Microseconds' = @{ Singular = 'microsecond' Plural = 'microseconds' - Abbreviation = 'usec' - Symbol = "$([char]0x00B5)s" + Abbreviation = 'µsec' + Symbol = "µs" Ticks = 10 } }