|
| 1 | +function Format-TimeSpan { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Format a TimeSpan object in the most relevant units of time. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Takes a TimeSpan object and returns a formatted string representing the duration in the most appropriate units (microseconds, milliseconds, seconds, minutes, hours, days). |
| 8 | +
|
| 9 | + .PARAMETER TimeSpan |
| 10 | + The TimeSpan object to format. |
| 11 | +
|
| 12 | + .PARAMETER Abbreviate |
| 13 | + If specified, unit labels will be abbreviated (e.g., "ms" instead of "milliseconds"). |
| 14 | +
|
| 15 | + .INPUTS |
| 16 | + TimeSpan |
| 17 | +
|
| 18 | + .OUTPUTS |
| 19 | + String representing the formatted timespan. |
| 20 | +
|
| 21 | + .EXAMPLE |
| 22 | + $TimeSpan = New-TimeSpan -Milliseconds 1500 |
| 23 | + Format-TimeSpan -TimeSpan $TimeSpan |
| 24 | +
|
| 25 | + Returns: "1.50 seconds" |
| 26 | +
|
| 27 | + .EXAMPLE |
| 28 | + Format-TimeSpan -TimeSpan ([TimeSpan]::Zero) -Abbreviate |
| 29 | +
|
| 30 | + Returns: "0s" |
| 31 | +
|
| 32 | + .EXAMPLE |
| 33 | + New-TimeSpan -Days 1 -Hours 2 -Minutes 30 -Seconds 0 | Format-TimeSpan |
| 34 | +
|
| 35 | + Returns: "1 day 2 hours 30 minutes" |
| 36 | +
|
| 37 | + .NOTES |
| 38 | + Microseconds or milliseconds are not included in the output if the timespan is 1 second or longer. |
| 39 | + Durations of 1 minute or longer do not show fractional (decimal) values. |
| 40 | + Zero-value components are omitted from multi-component output for cleaner formatting. |
| 41 | +
|
| 42 | + .LINK |
| 43 | + New-TimeSpan |
| 44 | + https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/new-timespan |
| 45 | + #> |
| 46 | + [CmdletBinding()] |
| 47 | + [OutputType([string])] |
| 48 | + param ( |
| 49 | + # The TimeSpan object to format. |
| 50 | + [Parameter(Mandatory = $true, ValueFromPipeline = $true, HelpMessage = 'The TimeSpan object to format.')] |
| 51 | + [TimeSpan]$TimeSpan, |
| 52 | + |
| 53 | + # Optional switch to abbreviate the unit labels in the output. |
| 54 | + [Parameter(Mandatory = $false, HelpMessage = 'If specified, unit labels will be abbreviated (e.g., "ms" instead of "milliseconds").')] |
| 55 | + [switch] $Abbreviate |
| 56 | + ) |
| 57 | + |
| 58 | + begin { |
| 59 | + if ($Abbreviate) { |
| 60 | + $MicrosecondLabel = 'µs' |
| 61 | + $MillisecondLabel = 'ms' |
| 62 | + $SecondLabel = 's' |
| 63 | + $MinuteLabel = 'm' |
| 64 | + $HourLabel = 'h' |
| 65 | + $DayLabel = 'd' |
| 66 | + } else { |
| 67 | + $MicrosecondLabel = ' microseconds' |
| 68 | + $MillisecondLabel = ' milliseconds' |
| 69 | + $SecondLabel = ' seconds' |
| 70 | + $MinuteLabel = ' minutes' |
| 71 | + $HourLabel = ' hours' |
| 72 | + $DayLabel = ' days' |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + process { |
| 77 | + $TotalMilliseconds = $TimeSpan.TotalMilliseconds |
| 78 | + |
| 79 | + # Handle zero timespan |
| 80 | + if ($TotalMilliseconds -eq 0) { |
| 81 | + "0$SecondLabel" |
| 82 | + } elseif ($TotalMilliseconds -lt 1) { |
| 83 | + "{0:N2}$MicrosecondLabel" -f ($TotalMilliseconds * 1000) |
| 84 | + } elseif ($TotalMilliseconds -lt 1000) { |
| 85 | + "{0:N2}$MillisecondLabel" -f $TotalMilliseconds |
| 86 | + } elseif ($TimeSpan.TotalSeconds -lt 60) { |
| 87 | + "{0:N2}$SecondLabel" -f $TimeSpan.TotalSeconds |
| 88 | + } elseif ($TimeSpan.TotalMinutes -lt 60) { |
| 89 | + # Build output with non-zero components only |
| 90 | + $Output = "{0:N0}$MinuteLabel" -f $TimeSpan.Minutes |
| 91 | + if ($TimeSpan.Seconds -gt 0) { |
| 92 | + $Output += " {0:N0}$SecondLabel" -f $TimeSpan.Seconds |
| 93 | + } |
| 94 | + $Output |
| 95 | + } elseif ($TimeSpan.TotalHours -lt 24) { |
| 96 | + # Build output with non-zero components only |
| 97 | + $Output = "{0:N0}$HourLabel" -f $TimeSpan.Hours |
| 98 | + if ($TimeSpan.Minutes -gt 0) { |
| 99 | + $Output += " {0:N0}$MinuteLabel" -f $TimeSpan.Minutes |
| 100 | + } |
| 101 | + if ($TimeSpan.Seconds -gt 0) { |
| 102 | + $Output += " {0:N0}$SecondLabel" -f $TimeSpan.Seconds |
| 103 | + } |
| 104 | + $Output |
| 105 | + } else { |
| 106 | + # Build output with non-zero components only |
| 107 | + $Output = "{0:N0}$DayLabel" -f $TimeSpan.Days |
| 108 | + if ($TimeSpan.Hours -gt 0) { |
| 109 | + $Output += " {0:N0}$HourLabel" -f $TimeSpan.Hours |
| 110 | + } |
| 111 | + if ($TimeSpan.Minutes -gt 0) { |
| 112 | + $Output += " {0:N0}$MinuteLabel" -f $TimeSpan.Minutes |
| 113 | + } |
| 114 | + if ($TimeSpan.Seconds -gt 0) { |
| 115 | + $Output += " {0:N0}$SecondLabel" -f $TimeSpan.Seconds |
| 116 | + } |
| 117 | + $Output |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments