-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathConvert-DurationStringToMs.ps1
More file actions
77 lines (60 loc) · 1.74 KB
/
Convert-DurationStringToMs.ps1
File metadata and controls
77 lines (60 loc) · 1.74 KB
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
function Convert-DurationStringToMs {
param(
[Parameter(Mandatory)]
[string]
$DurationString
)
$durationStack = @()
$unitStack = @()
$durationBuff = $false
$unitBuff = $false
for($i=0; $i -le $DurationString.Length; $i++){
$s = $DurationString[$i]
#Write-Host $s
if($s -match "\d|\."){ # consume if it is a number or a decimal
# init buffer
if($durationBuff -eq $false){
$durationBuff = ""
}
# accept last unit
if(-Not $unitBuff -eq $false){
$unitStack += $unitBuff
$unitBuff = $false
}
$durationBuff += $s
}else{ # otherwise it is a unit -- multiply by it to get the ms.
# init buffer
if($unitBuff -eq $false){
$unitBuff = ""
}
# accept last digit buffer
if(-Not $durationBuff -eq $false){
$durationStack += $durationBuff
$durationBuff = $false
}
$unitBuff += $s
}
}
# should always end with accepting the last one (because it will be a
# unit)
$unitStack += $unitBuff
$totalMs = 0
for($i=0; $i -le $unitStack.Length; $i++){
$time = [System.Convert]::ToDecimal($durationStack[$i])
$unit = $unitStack[$i]
if($unit -eq 'h'){
$time = $time * (60*60*1000)
}
if($unit -eq 'm'){
$time = $time * (60*1000)
}
if($unit -eq 's'){
$time = $time * (1000)
}
if($unit -eq 'ms'){
$time = $time
}
$totalMs += $time
}
return $totalMs
}