-
Notifications
You must be signed in to change notification settings - Fork 1
/
Get-ClosestToMinute.ps1
76 lines (64 loc) · 2.42 KB
/
Get-ClosestToMinute.ps1
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
function Get-ClosestToMinute {
<#
.synopsis
Returns an object with a timespan that is rounded to the nearest x minute.
.description
Returns an object with a timespan that is rounded to the nearest x minute,
specified by the -minute parameter, and rounding up or down depending on
the value passed to the -boundary parameter.
.example
PS C:\> Get-ClosestToMinute -timespan '00:12'
initialtimespan nearestminute boundary newtimespan
--------------- ------------- -------- -----------
00:12:00 15 5 00:15:00
.EXAMPLE
PS C:\> Get-ClosestToMinute -timespan 01:12 -minute 30 -boundary 20
initialtimespan nearestminute boundary newtimespan
--------------- ------------- -------- -----------
01:12:00 30 20 01:00:00
#>
[cmdletbinding()]
param (
# starting timespan to be rounded to the nearest x minute
[parameter(
mandatory,
valuefrompipeline,
valuefrompipelinebypropertyname
)]
[alias('initialtimespan', 'ts')]
[timespan] $timespan,
# the minute to round the timespan to
[parameter(valuefrompipeline)]
[alias('nearestminute')]
[int] $minute = 15,
# if we're outside the boundary, we aim to go to the next/prev $minute
[parameter(valuefrompipelinebypropertyname)]
[validatenotnullorempty()]
[validatescript({ $_ -ge 0 -and $_ -le $minute })]
[alias('increaseboundary')]
[int] $boundary = 5
)
process {
$newts = $timespan
$minutemod = $timespan.minutes % $minute
$tsmod = new-timespan -minutes $minutemod
$tsincrease = new-timespan -minutes ($minute - $minutemod)
$halfnearestminute = $minute / 2
write-psfmessage -level verbose -message "calculating new timespan for $newts"
$newts = if ($minutemod -gt $boundary -and $newts.totalminutes -ge $boundary) {
$newts.add($tsincrease)
}
elseif ($minutemod -le $halfnearestminute -and $newts.totalminutes -ge $halfnearestminute) {
$newts.subtract($tsmod)
}
else {
$newts.add($tsincrease)
}
[pscustomobject]@{
initialtimespan = $timespan
nearestminute = $minute
boundary = $boundary
newtimespan = $newts
}
}
}