-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathConvertTo-Seconds.psm1
132 lines (108 loc) · 3.51 KB
/
ConvertTo-Seconds.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<#
.SYNOPSIS
Converts unit to seconds.
.DESCRIPTION
This module converts a given time unit to seconds.
e.g hours to seconds.
More Information on https://github.com/Icinga/icinga-powershell-framework
.PARAMETER Value
Specify unit to be converted to seconds. Allowed units: ms, s, m, h, d, w, M, y
ms = miliseconds; s = seconds; m = minutes; h = hours; d = days; w = weeks; M = months; y = years;
Like 20d for 20 days.
.EXAMPLE
PS> ConvertTo-Seconds 30d
2592000
.LINK
https://github.com/Icinga/icinga-powershell-framework
.NOTES
#>
function ConvertTo-Seconds()
{
param(
[string]$Value
);
if ([string]::IsNullOrEmpty($Value)) {
return $Value;
}
[string]$NumberPart = '';
[string]$UnitPart = '';
[bool]$Negate = $FALSE;
[bool]$hasUnit = $FALSE;
foreach ($char in $Value.ToCharArray()) {
if ((Test-Numeric $char)) {
$NumberPart += $char;
} else {
if ($char -eq '-') {
$Negate = $TRUE;
} elseif ($char -eq '.' -Or $char -eq ',') {
$NumberPart += '.';
} else {
$UnitPart += $char;
$hasUnit = $TRUE;
}
}
}
if (-Not $hasUnit -Or (Test-Numeric $NumberPart) -eq $FALSE) {
return $Value;
}
[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) {
return $Value;
}
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 {
Throw $errorMsg;
break;
}
}
}
return $result;
}
function ConvertTo-SecondsFromIcingaThresholds()
{
param(
[string]$Threshold
);
[array]$Content = $Threshold.Split(':');
[array]$NewContent = @();
foreach ($entry in $Content) {
$NewContent += (Get-IcingaThresholdsAsSeconds -Value $entry)
}
[string]$Value = [string]::Join(':', $NewContent);
if ([string]::IsNullOrEmpty($Value) -eq $FALSE -And $Value.Contains(':') -eq $FALSE) {
return [convert]::ToDouble($Value);
}
return $Value;
}
function Get-IcingaThresholdsAsSeconds()
{
param(
[string]$Value
);
if ($Value.Contains('~')) {
$Value = $Value.Replace('~', '');
return [string]::Format('~{0}', (ConvertTo-Seconds $Value));
} elseif ($Value.Contains('@')) {
$Value = $Value.Replace('@', '');
return [string]::Format('@{0}', (ConvertTo-Seconds $Value));
}
return (ConvertTo-Seconds $Value);
}
Export-ModuleMember -Function @( 'ConvertTo-Seconds', 'ConvertTo-SecondsFromIcingaThresholds' );