mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-21 07:10:15 -05:00
Improved ConvertTo-Seconds Cmdlet
This commit is contained in:
parent
49b1beee5a
commit
6a2e4b94e5
1 changed files with 52 additions and 24 deletions
|
|
@ -1,31 +1,59 @@
|
||||||
|
Import-IcingaLib core\tools;
|
||||||
# year month week days hours minutes seconds milliseconds
|
# year month week days hours minutes seconds milliseconds
|
||||||
|
|
||||||
function ConvertTo-Seconds()
|
function ConvertTo-Seconds()
|
||||||
{
|
{
|
||||||
param(
|
param(
|
||||||
[string]$Value,
|
[string]$Value
|
||||||
[char]$Unit,
|
|
||||||
[switch]$Milliseconds
|
|
||||||
);
|
);
|
||||||
|
|
||||||
#100 D
|
[string]$NumberPart = '';
|
||||||
#100 D
|
[string]$UnitPart = '';
|
||||||
$Unit = $Value.Substring($Value.get_Length()-2)[0];
|
[bool]$Negate = $FALSE;
|
||||||
[single]$ValueSplitted = $Value;
|
|
||||||
#$Name.Substring($Name.get_Length()-1);
|
foreach($char in $Value.ToCharArray()) {
|
||||||
#$Name.Substring(0,$Name.get_Length()-1);
|
if ((Test-Numeric $char)) {
|
||||||
switch ([int][char]$Unit) {
|
$NumberPart += $char;
|
||||||
# { 'ms', 'milliseconds' -contains $_ } { $result = ($Value / [math]::Pow(10, 3)); $boolOption = $true; }
|
} else {
|
||||||
{ 115 -contains $_ } { $result = $ValueSplitted; $boolOption = $true; }
|
if ($char -eq '-') {
|
||||||
{ 109 -contains $_ } { $result = ($ValueSplitted * 60); $boolOption = $true; }
|
$Negate = $TRUE;
|
||||||
{ 104 -contains $_ } { $result = ($ValueSplitted * 3600); $boolOption = $true; }
|
} elseif ($char -eq '.' -Or $char -eq ',') {
|
||||||
{ 100 -contains $_ } { $result = ($ValueSplitted * 86400); $boolOption = $true; }
|
$NumberPart += '.';
|
||||||
{ 87 -contains $_ } { $result = ($ValueSplitted * 604800); $boolOption = $true; }
|
} else {
|
||||||
{ 77 -contains $_ } { $result = ($ValueSplitted * (2.5[math]::Pow(10, 6))); $boolOption = $true; }
|
$UnitPart += $char;
|
||||||
{ 89 -contains $_ } { $result = ($ValueSplitted * (3.10[math]::Pow(10, 7))); $boolOption = $true; }
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[single]$ValueSplitted = $NumberPart;
|
||||||
|
$result = 0;
|
||||||
|
|
||||||
|
if ($Negate) {
|
||||||
|
$ValueSplitted *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
[string]$errorMsg = (
|
||||||
|
[string]::Format('Invalid unit type "{0}" specified for convertion. Allowed units: ms, s, m, h, d, w, M, y', $UnitPart)
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($UnitPart -Match 'ms') {
|
||||||
|
$result = ($ValueSplitted / [math]::Pow(10, 3));
|
||||||
|
} else {
|
||||||
|
if ($UnitPart.Length -gt 1) {
|
||||||
|
Throw $errorMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ([int][char]$UnitPart) {
|
||||||
|
{ 115 -contains $_ } { $result = $ValueSplitted; break; } # s
|
||||||
|
{ 109 -contains $_ } { $result = $ValueSplitted * 60; break; } # m
|
||||||
|
{ 104 -contains $_ } { $result = $ValueSplitted * 3600; break; } # h
|
||||||
|
{ 100 -contains $_ } { $result = $ValueSplitted * 86400; break; } # d
|
||||||
|
{ 119 -contains $_ } { $result = $ValueSplitted * 604800; break; } # w
|
||||||
|
{ 77 -contains $_ } { $result = $ValueSplitted * 2592000; break; } # M
|
||||||
|
{ 121 -contains $_ } { $result = $ValueSplitted * 31536000; break; } # y
|
||||||
default {
|
default {
|
||||||
if (-Not $boolOption) {
|
Throw $errorMsg;
|
||||||
Throw 'Invalid input';
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue