Skip to content

Commit c24d9db

Browse files
committed
refactor: Simplify output handling in Format-TimeSpan function
1 parent 54f4648 commit c24d9db

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

General/Format-Timespan.ps1

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -80,45 +80,38 @@
8080
process {
8181
$TotalMilliseconds = $TimeSpan.TotalMilliseconds
8282

83-
# Handle zero timespan
8483
if ($TotalMilliseconds -eq 0) {
85-
"0$SecondLabel"
84+
# Handle zero timespan
85+
$Output = "0$SecondLabel"
86+
8687
} elseif ($TotalMilliseconds -lt 1) {
87-
"{0:N2}$MicrosecondLabel" -f ($TotalMilliseconds * 1000)
88+
$Output = "{0:N2}$MicrosecondLabel" -f ($TotalMilliseconds * 1000)
89+
8890
} elseif ($TotalMilliseconds -lt 1000) {
89-
"{0:N2}$MillisecondLabel" -f $TotalMilliseconds
91+
$Output = "{0:N2}$MillisecondLabel" -f $TotalMilliseconds
92+
9093
} elseif ($TimeSpan.TotalSeconds -lt 60) {
91-
"{0:N2}$SecondLabel" -f $TimeSpan.TotalSeconds
94+
$Output = "{0:N2}$SecondLabel" -f $TimeSpan.TotalSeconds
95+
9296
} elseif ($TimeSpan.TotalMinutes -lt 60) {
9397
# Build output with non-zero components only
9498
$Output = "{0:N0}$MinuteLabel" -f $TimeSpan.Minutes
95-
if ($TimeSpan.Seconds -gt 0) {
96-
$Output += " {0:N0}$SecondLabel" -f $TimeSpan.Seconds
97-
}
98-
$Output
99+
if ($TimeSpan.Seconds -gt 0) { $Output += " {0:N0}$SecondLabel" -f $TimeSpan.Seconds }
100+
99101
} elseif ($TimeSpan.TotalHours -lt 24) {
100102
# Build output with non-zero components only
101103
$Output = "{0:N0}$HourLabel" -f $TimeSpan.Hours
102-
if ($TimeSpan.Minutes -gt 0) {
103-
$Output += " {0:N0}$MinuteLabel" -f $TimeSpan.Minutes
104-
}
105-
if ($TimeSpan.Seconds -gt 0) {
106-
$Output += " {0:N0}$SecondLabel" -f $TimeSpan.Seconds
107-
}
108-
$Output
104+
if ($TimeSpan.Minutes -gt 0) { $Output += " {0:N0}$MinuteLabel" -f $TimeSpan.Minutes }
105+
if ($TimeSpan.Seconds -gt 0) { $Output += " {0:N0}$SecondLabel" -f $TimeSpan.Seconds }
106+
109107
} else {
110108
# Build output with non-zero components only
111109
$Output = "{0:N0}$DayLabel" -f $TimeSpan.Days
112-
if ($TimeSpan.Hours -gt 0) {
113-
$Output += " {0:N0}$HourLabel" -f $TimeSpan.Hours
114-
}
115-
if ($TimeSpan.Minutes -gt 0) {
116-
$Output += " {0:N0}$MinuteLabel" -f $TimeSpan.Minutes
117-
}
118-
if ($TimeSpan.Seconds -gt 0) {
119-
$Output += " {0:N0}$SecondLabel" -f $TimeSpan.Seconds
120-
}
121-
$Output
110+
if ($TimeSpan.Hours -gt 0) { $Output += " {0:N0}$HourLabel" -f $TimeSpan.Hours }
111+
if ($TimeSpan.Minutes -gt 0) { $Output += " {0:N0}$MinuteLabel" -f $TimeSpan.Minutes }
112+
if ($TimeSpan.Seconds -gt 0) { $Output += " {0:N0}$SecondLabel" -f $TimeSpan.Seconds }
122113
}
114+
115+
$Output
123116
}
124117
}

0 commit comments

Comments
 (0)