functions/uadashfunctions.ps1
function Format-UASecondsToReadableString { <# .SYNOPSIS Takes a count of seconds and converts to a "Human Readable" time string (X Hours, X Minutes, X Seconds) .DESCRIPTION Takes a count of seconds and converts to a timespan object. From here we evaluate to build a formatted "Human Readable" time string (X Hours, X Minutes, X Seconds) .INPUTS Double Seconds .OUTPUTS System.String. A formatted human readable string suitable for grids. .EXAMPLE PS> Format-SecondsToReadableString 540 9 Minutes .EXAMPLE PS> Format-SecondsToReadableString 541 9 Minutes 1 Second #> Param( [Parameter(Mandatory=$true)] [Double] $Seconds ) $TimeSpan = [timespan]::fromseconds($Seconds) #Singular Formatting if($TimeSpan.Hours -eq 1) { $HourString = "1 Hour" } elseif($TimeSpan.Hours -eq 0) { $HourString = "" } else { $HourString = ($TimeSpan.Hours.ToString() + " Hours") } if($TimeSpan.Minutes -eq 1) { $MinuteString = "1 Minute" } elseif($TimeSpan.Minutes -eq 0) { $MinuteString = "" } else { $MinuteString = ($TimeSpan.Minutes.ToString() + " Minutes") } if($TimeSpan.Seconds -eq 1) { $SecondString = "1 Second" } elseif($TimeSpan.Seconds -eq 0) { $SecondString = "" } else { $SecondString = ($TimeSpan.Seconds.ToString() + " Seconds") } if ($TimeSpan.Hours -eq 0 -and $TimeSpan.Minutes -eq 0 -and $TimeSpan.Seconds -eq 0) { $DurationTimeString = "Less than 1 Second" } elseif ($TimeSpan.Seconds -lt 0) { $DurationTimeString = "Less than 1 Second" } elseif($TimeSpan.Hours -gt 0) { $DurationTimeString = ($HourString + " " + $MinuteString) } elseif($TimeSpan.Minutes -gt 0) { $DurationTimeString = ($MinuteString + " " + $SecondString) } else { $DurationTimeString = ($SecondString) } return $DurationTimeString } |