2019-08-20 11:01:20 -04:00
|
|
|
Import-IcingaLib core\tools;
|
|
|
|
|
|
|
|
|
|
function ConvertFrom-TimeSpan()
|
|
|
|
|
{
|
2021-05-07 08:38:10 -04:00
|
|
|
param (
|
|
|
|
|
$Seconds = 0
|
2019-08-20 11:01:20 -04:00
|
|
|
);
|
|
|
|
|
|
2021-05-31 03:50:13 -04:00
|
|
|
if (([string]$Seconds).Contains(',') -Or (Test-Numeric $Seconds)) {
|
|
|
|
|
[decimal]$Seconds = [decimal]([string]$Seconds).Replace(',', '.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$Sign = '';
|
|
|
|
|
if ($Seconds -lt 0) {
|
|
|
|
|
$Seconds = [math]::Abs($Seconds);
|
|
|
|
|
$Sign = '-';
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 10:20:45 -04:00
|
|
|
if ((Test-Numeric $Seconds) -eq $FALSE) {
|
|
|
|
|
return $Seconds;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 11:01:20 -04:00
|
|
|
$TimeSpan = [TimeSpan]::FromSeconds($Seconds);
|
|
|
|
|
|
2021-05-07 08:38:10 -04:00
|
|
|
if ($TimeSpan.TotalDays -ge 1.0) {
|
|
|
|
|
return (
|
|
|
|
|
[string]::Format(
|
2021-05-31 03:50:13 -04:00
|
|
|
'{0}{1}d',
|
|
|
|
|
$Sign,
|
2021-05-28 18:07:25 -04:00
|
|
|
([math]::Round($TimeSpan.TotalDays, 2))
|
2021-05-07 08:38:10 -04:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ($TimeSpan.TotalHours -ge 1.0) {
|
|
|
|
|
return (
|
|
|
|
|
[string]::Format(
|
2021-05-31 03:50:13 -04:00
|
|
|
'{0}{1}h',
|
|
|
|
|
$Sign,
|
2021-05-28 18:07:25 -04:00
|
|
|
([math]::Round($TimeSpan.TotalHours, 2))
|
2021-05-07 08:38:10 -04:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ($TimeSpan.TotalMinutes -ge 1.0) {
|
|
|
|
|
return (
|
|
|
|
|
[string]::Format(
|
2021-05-31 03:50:13 -04:00
|
|
|
'{0}{1}m',
|
|
|
|
|
$Sign,
|
2021-05-28 18:07:25 -04:00
|
|
|
([math]::Round($TimeSpan.TotalMinutes, 2))
|
2021-05-07 08:38:10 -04:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ($TimeSpan.TotalSeconds -ge 1.0) {
|
|
|
|
|
return (
|
|
|
|
|
[string]::Format(
|
2021-05-31 03:50:13 -04:00
|
|
|
'{0}{1}s',
|
|
|
|
|
$Sign,
|
2021-05-28 18:07:25 -04:00
|
|
|
([math]::Round($TimeSpan.TotalSeconds, 2))
|
2021-05-07 08:38:10 -04:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-05-31 03:50:13 -04:00
|
|
|
if ($TimeSpan.TotalMilliseconds -ge 1.0) {
|
2021-05-07 08:38:10 -04:00
|
|
|
return (
|
|
|
|
|
[string]::Format(
|
2021-05-31 03:50:13 -04:00
|
|
|
'{0}{1}ms',
|
|
|
|
|
$Sign,
|
2021-05-07 08:38:10 -04:00
|
|
|
$TimeSpan.TotalMilliseconds
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 03:50:13 -04:00
|
|
|
if ($Seconds -lt 0.001) {
|
|
|
|
|
return ([string]::Format('{0}{1}us', $Sign, ([math]::Ceiling([decimal]($Seconds*[math]::Pow(10, 6))))));
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 08:38:10 -04:00
|
|
|
return ([string]::Format('{0}s', $Seconds));
|
2019-08-20 11:01:20 -04:00
|
|
|
}
|